Unity hangs when trying to load an image from url using the WWW class

Unity version: 5.4.1 Platform: Windows

I am trying to load an image from a url into a texture using the WWW.LoadImageIntoTexture() method. Whenever I do this though, the game will freeze for about half a second to a full second before resuming normally. Here is the code:

IEnumerator GetTexture () {
		Texture2D tex = new Texture2D (4, 4, TextureFormat.DXT1, false);
		WWW www = new WWW (url);
		yield return www;
		www.LoadImageIntoTexture (tex);
        Renderer imageMAT = GetComponent<Renderer>();
		int height = imageMAT.material.GetTexture("_Main").height;
		int width = imageMAT.material.GetTexture("_Main").width;
		float verticalScale = (float)height / (float)width;
		gameObject.transform.localScale = new Vector3 (1f, verticalScale, 1f);

Where ‘url’ is a string that directs to an image online.

It’s to my understanding that WWW is supposed to be asynchronous (correct me if I’m wrong in assuming this) but what’s really puzzling me are the results I’m getting from the Unity Profiler. When I use the profiler, I see there is a clear spike where the frame takes 571 ms to finish but according to the profiler, the thing that is taking so long is called “iTween.SetupCoroutine()[Coroutine: InvokeMoveNext]”.

I am using iTween in this project, but not anywhere in this script. I looked through the iTween script and I couldn’t find a method called “SetupCoroutine” or anything called “InvokeMoveNext”. It is also worth mentioning that when I select that line in the profiler to view details in the window on the right, the ‘Object’ column tells me that the object calling ‘iTween.SetupCoroutine’ is ‘N/A’.

According to this, the hang was half a second. I added in some Debug statements to see what the difference in Time.time was between completion of the www call and the LoadImageIntoTexture line and the result was that the www call took a little over 1 second to complete. So if the hang was only half of that and WWW is supposed to be asynchronous, then I don’t think that the www call is the reason for the hang, but then I have no idea what could be causing it. The ‘www.LoadImageIntoTexture(tex)’ line happens instantaneously, so I don’t think it’s that either.

Any ideas on what could be causing this or what the iTween.SetupCoroutine is referring to, or at least where I should try looking next? Any guidance is greatly appreciated, thanks!

WWW is asynchronous. At the moment it’s not clear where you start that coroutine you’ve shown. You should create a “deep profile” (enable the toggle at the top) and examine further down what might cause this spike. Note that your coroutine does some heavy things after the request has finished.

LoadImageIntoTexture might has to decompress and recompress the texture as you want it as DXT1

using “imageMAT.material” will duplicate the material that the renderer currently uses. It might have an impact if the object was batched before.

(Why do you use “GetTexture” when you can simply use tex.height and tex.width ?)

Changing the scale of an object can have an impact depending on the object, For example the physics system has to rebuild the collider. It the object as marked as static this can have a huge impact as this shouldn’t be done at all ^^.

In the end you have to first track down the actual offending source by looking at the deep profile.