Asynchronously loading large images from url

Is it just not possible to download a large 2048x2048 image into a texture in Unity3D without stutter?

I’m using a coroutine and the WWW class to do the download, however the WWW.GetTexture function shows up on the profiler taking 235ms on a PC.

It gets even worse if I want to create a texture with mipmaps as just constructing the Texture2D takes 450ms before using LoadImageIntoTexture or texture.SetPixels etc…

Surely there has to be a way to create textures (that are not being rendered yet) and upload to GPU asynchronously.

Anyone got any ideas? I’m assuming that I can’t create a thread as Unity’s API is not thread safe.

The problem is, Unity will always load the entire image in memory when you create a Texture2D, no matter the method used. This takes time, and there’s no way to avoid it. It won’t parse the file and get bits of the image data, or load slowly per frame. This happens with any instantiation of anything in Unity, be it images, terrain, objects created by Instantiate(), etc.

If you require only the image data for some processing, I would suggest using a library like libjpeg or libpng (in a C# rendition of it) to get the data in another thread (you can use another thread as long as you don’t invoke Unity methods), but if you have to display it, I don’t see a way you’re going to stop the lag.

Two things you could try, and I’m not sure it would render any results, is to create an array of Color32 containing the image data by using an external lib as I said before, and try to create a texture to it, see if it is faster than Texture2D creation via WWW, or you could try to setup a scene with your loading script, and call Application.LoadLevelAdditiveAsync() to load your scene, and see if it executes your script and create your Texture2D async or not (not sure a script will run before or after the scene is loaded async).