Answer the question
In order to leave comments, you need to log in
How to implement implement resource loader from web in Unity?
I have two tasks:
1. I want to download a text file from the web, but I don't understand how to write a class that will have a static method that takes a url and eventually returns the downloaded content. Namely, it is not clear to me how to do this with IEnumerator, considering that it does not return anything when it is executed.
2. I would like to load during game initialization. The test file will contain configs for everything (player speed, etc.). And as far as I understand, IEnumerator execution is not a blocking operation. How to implement?
Answer the question
In order to leave comments, you need to log in
As for static, it’s up to you to decide, but in general it’s easiest to pass a callback to coroutines:
using UnityEngine;
using System;
using System.Collections;
public class Example : MonoBehaviour
{
private void Start()
{
StartCoroutine(DownloadFile("url", text => DeserializeConfig(text, () => Debug.Log("Done"))));
}
private IEnumerator DownloadFile(string url, Action<string> onComplete)
{
using (WWW www = new WWW(url))
{
yield return www;
onComplete(www.text);
}
}
private void DeserializeConfig(string text, Action onComplete)
{
Debug.Log(text);
onComplete();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question