I'm trying to load textures in the background using WWW. Here is a snippet. Code (CSharp): void Start(){ StartCoroutine("LoadBgImage"); } private IEnumerator LoadBgImage(){ string path = volume.BackgroundImageUrl; WWW www = new WWW (path); Debug.Log ("Do the request"); yield return www; Debug.Log ("request complete"); Texture2D tex = www.texture; } This works on desktop, but when I try on iOS the application halts at the yield return statements.
What do you mean it halts ? It crashes? What does your log says? It can be a problem of URL, you do not check after the request that the www has been successful (check for www.errors).
When I say it halts. I mean the log on line 12 never runs. The log on line 10 does. This is also the case if I yield return null.
That's the part that makes this behavior really strange, and makes it possible you've encountered a Unity bug. Try this test case. What is its output? Code (csharp): void Start() { StartCoroutine(Tester1()); StartCoroutine("Tester2"); } public IEnumerator Tester1() { Debug.Log("Tester1a"); yield return null; Debug.Log("Tester1b"); } public IEnumerator Tester2() { Debug.Log("Tester2a"); yield return null; Debug.Log("Tester2b"); } (Checking to see if there's a difference between the string version and the "natural" version of StartCoroutine. I use the natural version all the time on iOS with no issues, but I've never used the string version on iOS, so it's possible the difference is between those variants.) If you put this on an object in your scene and see anything less than all four Debug statements, then I would say you should use it as a repro case for a bug report to UT. Finally, which version of Unity are you on?
I'm on version 4.5.2. The output of those couroutines is complete Tester1a Tester1b Tester2a Tester2b
Something else is going on. Your script works fine on my iPad. You might want to check your URL, your network connection, etc. I copied your script and modified it with my own URL and got it to work just fine in an empty Unity scene with a camera and the script hung on that camera. I'm using Unity 4.5.2f1 and XCode 5.0.2. Kurt Code (csharp): usingUnityEngine; usingSystem.Collections; //10:07AM9/18/2014 //http://forum.unity3d.com/threads/coroutines-halting-on-ios.269220/ publicclassForum269220 : MonoBehaviour { Texture2Dtex; voidStart() { StartCoroutine("LoadBgImage"); } privateIEnumeratorLoadBgImage() { stringpath = "http://www.plbm.com/private/launchprocedure.jpg"; WWWwww = newWWW (path); Debug.Log ("Do the request"); yieldreturnwww; Debug.Log ("request complete"); tex = www.texture; } voidOnGUI() { Rectr = newRect (0, 0, Screen.width, 0); if (tex != null) { r.height = (r.width * tex.height) / tex.width; GUI.DrawTexture( r, tex); } else { r.height = Screen.width * 0.10f; GUI.Label ( r, "Loading..."); } } }
This isn't being cause by the URL. This coroutine is being launched right after a new scene is loaded. And it doesn't matter what is in the coroutine, it halts on the first yield.
Try making start() return an Ienumerator too (that makes unity treat it as a coro), then do a yield for 1 sec before issuing your www request... Just poking in the dark with that idea... Kurt
Instead of doing yield return www, you make be able to get better information by writing your own yielding loop. For one example: Code (csharp): WWW www = new WWW("path"); while (www.texture == null) { Debug.Log("Progress: "+www.progress.ToString("#.00")); if (www.error != "" && www.error != null) Debug.LogWarning("Error: "+www.error); //any other information you want to test it with, can go here yield return 0; } Debug.Log("Finished, with a texture");