- Home /
Does anyone have a solution for async image loading from url?
I have a scroll view with image thumbnails where images are loaded from an url with www class, and the scroll is laggy (like expected). I load images like this, on every item in the scrollview:
IEnumerator LoadImageCoroutine() {
WWW www = new WWW(imageUrl);
yield return www;
//img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
if (string.IsNullOrEmpty(www.error)) {
www.LoadImageIntoTexture (texture);
img.sprite = Sprite.Create (texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
DestroyImmediate(www.texture);
}
www.Dispose ();
Resources.UnloadUnusedAssets();
}
When the item goes out of the view, I disable the coroutines on it, and when it is a few items away from apearing, i call the LoadImageCoroutine(). Something like infinite scroll with loading the next and previous items. The problem is that it is all done on the UI thread, and its killing the scroll.
Thanks and best regards to all!
Answer by Bunny83 · Oct 10, 2017 at 06:56 PM
Actually the loading of the WWW class is carried out on a seperate thread. However the actual texture creation has to be done on the main thread.
However there are a few things you should change. First of all you use LoadImageIntoTexture which is fine. However this line:
DestroyImmediate(www.texture);
is completely unnecessary and just adds a lot overhead. When you actually access the texture property, Unity creates a Texture2D out of the data that has been loaded which you destroy immediately. LoadImageIntoTexture actually avoids the internal creation inside the WWW class as it directly exchanges the content of an existing texture.
Don't call Resources.UnloadUnusedAssets();
after every image load. That method has quite some overhead as it has to check all currently loaded assets and if they are referenced somewhere. If you actually run into trouble (which you shouldn't when you destroy the assets you don't use anymore) you may call it from time to time.
Keep in mind that Textures are objects that are derived from UnityEngine.Object. Any such object need to be actually destroyed with Destroy when you don't need it anymore. If you don't it remains in memory and can actually be "found" again with FindObjectsOfType. When such an asset is actually not referenced by anything in the game, calling UnloadUnusedAssets will destroy them, however at a much higher cost. If you simply keep track of when you are creating textures and when you're destroying them you should be fine.
You shouldn't touch www.texture at all. So make sure you actually change your Sprite.Create line. Again accessing www.texture will create one.
Your answer
Follow this Question
Related Questions
Cannot load image from URL passed as parameter 0 Answers
Asyncchronous image loading 2 Answers
Load image from url in Webplayer 1 Answer
Instantiate many Prefabs / Async Loading 3 Answers
URL Escaping for Firebase 2 Answers