- Home /
Loading a large external image to texture without freezing the game?
I'm building an application with Unity that loads large PNG images from a nearby folder. To do this I use the WWW class (http://docs.unity3d.com/ScriptReference/WWW.html). After that I load the image file into a texture, which I assign to a Sprite as such:
Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, true);
wwwObject.LoadImageIntoTexture(tex);
Sprite spr = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), 100);
Uses http://docs.unity3d.com/ScriptReference/WWW.LoadImageIntoTexture.html
All this works nice and dandy but it freezes the game's main thread while it's presumably loading and then compressing PNG files with resolutions spanning from 1080p to 4K. I've tried to look for ways to do this in a background thread or something but the Unity API is thread-unsafe and very pissy about that. Is there a way to load these images without freezing my game?
Answer by supernat · Jan 13, 2015 at 02:26 AM
I believe the WWW class is already creating a Texture2D for you, so you should be able to just use the wwwObject.texture (or wwwObject.textureNonReadable) variable instead of creating a new Texture2D, unless as the documentation states that you need to download a different texture continuously. Check out http://docs.unity3d.com/ScriptReference/WWW-texture.html
Another thing to keep in mind is that when you call LoadImageIntoTexture, it's going to resize the texture you pass in every time, since you are defining it as 4x4. It is more optimized to be able to replace a texture of the same size as the data, so that could be part of your slow down.
Alternatively, if the wwwObject.texture variable doesn't work for you for some reason, you can create your own coroutine, and load the data from the wwwObject manually, say 1 row at a time, each frame, until the Texture2D is fully copied, then have some callback at the end of the coroutine to set the texture. During this time, you could set a progress indication.
EDIT: Also note that you should use the WWW.textureNonReadable variable if you don't need to read pixel data from the texture.
Wonder why I missed the fact www objects already made their own Texture2D. However they game still freezes when I try to read the wwwObject.texture / .textureNonReadable. I timed it as such:
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
Texture2D tex = file.textureNonReadable;
stopwatch.Stop();
Debug.Log("Time to load: " + stopwatch.Elapsed$$anonymous$$illiseconds + "ms");
The various images I have take from 50ms to even 360ms in this piece of code. So my next question is, do you have an example of loading a Texture2D from the wwwObject through a coroutine?
Unfortunately, it's very complicated because you have to implement the image decompression algorithms on the raw image data you downloaded (wwwObject.bytes). But the idea would be to use those bytes and decompress a portion of the image into the Texture2D each frame while yielding, until the whole image is in the Texture2D and it is ready to use. In theory, you could maybe find a jpg/png decompressor library on the asset store (I haven't looked), and you could decompress the raw bytes in a separate thread into a new buffer of just bytes, then copy the entire contents of the new buffer into the Texture2D once the decompression is complete (I'm guessing the decompression is the part taking the longest).
Hmm, ok, so what I guess the workflow would look like: create www object, pass it to a separate thread, read bytes and decompress to raw image data (without using Unity API). Pass it back to the main thread, create a Texture2D of the same size as the original image, use Texture2D.SetPixels function in coroutine to gradually build up the texture. Correct?
@Clavus Ok this is old, but still a thing even in Unity 2018... Did anyone do this?
I think nowadays this is the solution: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html
Your answer
