I am downloading a resource from my server (approx. 4mb in size) and the download is blocking the main thread. Here’s the minimal code:
// Part of a MonoBehaviour script...
IEnumerator Foo()
{
// Actual URL removed for privacy...
WWW w = new WWW("http://myserver.com/myscript.php");
yield return w;
Debug.Log(w.text);
}
void LoadResource()
{
StartCoroutine(Foo());
}
void Update()
{
Debug.Log("Hello");
}
The GUI freezes (can’t even stop playback in the Editor) and the Update doesn’t get called again until the download has finished.
Try a busy wait construct where you yield return null and instead check the .isDone flag on your WWW object… might work better. Something like:
using( WWW w = new WWW("http://myserver.com/myscript.php"))
{
while( !w.isDone)
{
yield return null;
}
Debug.Log(w.text);
}
Remember to wrap your WWW (and any other object implementing the IDisposable interface) in a using construct, or else try/finally/.Dispose it manually.
However, the update and while debug logs get about 50 iterations but then it freezes until the download is finished. Interestingly, the isDone is set to true just before the freeze, as if it’s finished downloading but it hasn’t actually finished and blocks until it’s finished.