Preventing App freeze when loading a pic with WWW

Hello,

Im using WWW to load several pics from a server at different times during my WebGL app. My app is constantly running out of memory, and Im already at 750 Mbytes in the player settings. So im forced to load and unload pics to save memory, but now its causing freeze when loading pics.

The whole app freezes when its loading the pics, and it has to load several, so it looks and feels really bad to the user, and its not smooth.

Is there anyway to prevent the freeze, I dont know if the freeze is caused by the WWW or the code compressing the texture after its loaded to save memory. Anyway here is my code below, its actually in a CoRoutine that is called in a loop outside of that to load several pics at once, and this is done several times at random times. The whole thing freezes during the process, it does not crash, it resumes normally afterwards. Maybe there is a way to speed it up or not freeze?

Any help is extremely appreciated. Thanks!

url= [link to server to the pic]

WWW loader = new WWW(url, form);
yield return loader;

loader.texture.Compress(false);
loader.texture.Apply();

Texture2D temp = loader.texture;
Texture2D Tex = new Texture2D(temp.width, temp.height, TextureFormat.DXT1, false);
loader.LoadImageIntoTexture(Tex);

DestroyImmediate(pic.GetComponent().material.mainTexture);
pic.GetComponent().material.mainTexture = null;
pic.GetComponent().material.mainTexture=Tex;

DestroyImmediate(loader.texture);
loader.Dispose();
loader = null;

You should profile this code to see which part of it is actually the bottleneck.

LoadImageIntoTexture might be CPU-intensive. You could try to reduce the size of the Texture2D or change texture format to see if that makes any difference.

Anyway, as @liortal recommended, you should profile it.

@Marco-Trivellato are there any alternatives? for example - using the newer UnityWebRequest API? or maybe another method that can work on a different thread to produce the Texture (without blocking the scripting thread) ?

I don’t this this is related to WWW vs WebRequest.
I suspect that getting textures from asset bundles will speed things up but, again, you may want to profile it.

well, it is slightly related, since WWW.texture already gives you a texture object (that was created on another thread maybe?) and could possibly be faster than creating a texture and loading data into it…

I suppose there could be other APIs to do this, the main thing is not to block the scripting thread because that is what causes “hiccups” to the game.

Hello,

I have confirmed the freeze has to do with the WWW, this simple code will freeze the whole WebGL app in the browser for a short time each time its called (even freezes running in the editor)

IEnumerator loadPic(string picURL)
{
WWW loader=new WWW(picURL);
yield return loader;
loader=null;
}

Even if I do nothing with the texture like shown above, it still freezes.

Since im loading maybe 5 images or more at the same time by calling this coroutine multiple times in a row, it causes a large freeze in the entire app, for example the player is running and he would freeze mid run while the textures are being loaded, its really horrible, almost unplayable…

I really hope there is a work around, anybody knows?

Is it possible to do it in a separate thread in WebGL, maybe other API, or maybe using javascript?

Thanks

I tried using WebRequest, but it gives error

Texture2D t;
UnityWebRequest www = UnityWebRequest.GetTexture(url);
www.SetRequestHeader(;
AsyncOperation async=www.Send();
while (!async.isDone) {
yield return null;
}
if (www.isError) {
Debug.Log(www.error);:wink:
}
else {
t = DownloadHandlerTexture.GetContent(www); <---------- error on this line
}

Error m_InstanceID != 0
UnityEngine.Networking.DownloadHandlerTexture:GetContent(UnityWebRequest)

So I guess im screwed, WWW gives hiccup and WebRequest dont work at all!

I am the only person in the world using Unity to download a texture?

I am using UnityWebRequest.Get() instead of UnityWebRequest.GetTexture().
UnityWebReqeust.GetTexture() was unstable. Sometimes I’ve got crash on android platform last year.
I don’t know this problem has been solved.

var texture=newTexture2D(2,2,TextureFormat.RGB24,false);
texture.LoadImage(downloadHandler.data);

I don’t remember seeing any bug report related to this problem, so likely not.

I forgot to mention that 750mb seems a bit too much. You may want to profile your memory usage.

Its strange that when I play a video using WebGLMovieTexture, there is no hiccup in gameplay, but when i load an image texture using WWW the whole game freeze for a second while its loading the texture (using coroutine).

I would think a video would cause a bigger freeze than loading a single texture, but the video causes zero freeze, but loading just one texture causes whole game to freeze for a second. Its really annoying when loading lots of textures.

The problem i have is WWW freezing the UI for a while when i loaded a texture from internet. I found out the problem is 100% cause by the “LoadImage” function inside the WWW class.

This comment has a screen shot of the profiler

https://forum.unity.com/threads/problems-with-unity-freezing-when-loading-textures-from-internet.555319/

What is needed is an async version of LoadImage function, people been requesting that from way back in 2014

https://feedback.unity3d.com/suggestions/async-texture2d-dot-loadimage-and-other-texture-operations

LoadImage is running on the main thread this is causing the freeze.

According to this article

https://docs.unity3d.com/Manual/AsyncTextureUpload.html

There is an async LoadTexture already exist, but as you can see when i run the profiler it didnt work when i loaded the image using WWW from a website URL. Im building for native PC

https://forum.unity.com/threads/pro…g-when-loading-textures-from-internet.555319/

How can i turn on async LoadImage in my case?

Unfortunately the AsyncTextureUpload feature only applies to resources that are built with the project, not once that are loaded at runtime. I stream all my image resources from tiled image servers so I understand your frustration :(.

“When the project is built, the texture data of asynchronous uploadable textures are stored in as streaming resource files and are loaded asynchronously.”

Might be a bit of a drastic suggestion, but if you have full control of the image resources you could store the raw texture data and load that directly.

i.e. load your image, save the byte[ ] returned from texture.GetRawTextureData(), store it on the server.
Then when you download the image use texture.LoadRawTextureData().

This way the main thread doesn’t get stuck having to decode the images. Keep in mind the raw texture data is uncompressed and will be a lot larger, so you could apply gzip compression and then use the Content-Encoding header to let the browser handle the decompression (so Unity doesn’t stutter from having to do that)

Although I’m guessing if you already knew up front exactly which images you need to load you wouldn’t need to load them externally - unfortunately I don’t really know of a faster method besides writing your own jslib implementation.

1 Like

OMG, GENIUS!!!

Thanks so much!!!

I finally tried using texture.LoadRawTextureData with uncompressed images, unfortunately that causes stuttering too. Cant believe in 2019 with all the horse power and giga hertz of todays GPU and CPU this problem can still exist.

This is such a HUGE problem, i wish unity would fix it, they keep adding more and more fancy features with pipelines and threads that cant interact with the UI, more features here and there, but they should not forget improving the basic features also like this issue.

This problem happens in both WebGL and PC standalone apps.

My app downloads almost all its textures on the fly, they are totally random, cant tell which ones before hand. Sometimes it can download 20 textures in parallel from the server, and more at random times in the game depending on what the player does.

I thought maybe its possible to use worker threads, passing the URL of the image from C# to javascript, letting javascript download and decode the image converting the decoded image to base64, then passing that giant string back to c#, and then showing it, but dont know if that will work because seems texture.loadRawData still freeze.

This is really spoiling my Unity app because its stuttering like mad all over the place, it looks like an earthquake happening in the game, cant release it like that.

This is the single biggest issue i have, i would gladly pay someone for a solution!

2 Likes

You can’t load textures from managed memory (i.e., anything originating in C# or WASM) without stuttering. It’s just a limitation of the Emscripten runtime.

Without the Unity source code, it’s going to be too ambitious to use the browser’s JPEG to texture facilities, and passing that WebGL-ready texture handle to the engine. But that’s the path forward.

You can always use native code to update texture content, instead of using unity’s built-in methods, if loading lots of textures is must have.

In WebGL case, create new empty texture in unity, get it’s pointer, and use WebGLRenderingContext.texSubImage2D() on the native part, to draw image that you downloaded on the browser.
You can create html object, and register to it’s onLoad event, so once it loaded an image, you can use the texSubImage2D method.

750mb is A LOT for a WebGL App. I would first test uploading and downloading highly compressed images before assuming there is a bug with the WWW module.

Test your textures extensively, as if you running out of memory, most of the time it will be from loading textures.

Have this issue on parallel downloading ~15 small images with UnityWebRequest.
Interesting observation: the issue occurs only when compiling with unity 2019. In 2018 version everything seems to be ok