Hi, I wanna ask about www.texture. I use www.texture to load about 50 textures. Sometimes I destroy the mainTexture of that objects and sometimes I need to load it again (like paging system on webpage). Here’s part of my code :
At first load, I use variable “temp” to save www.texture, and tempHeight + tempWidth to save it’s resolution. But at the second load I don’t call that again, I only create new Texture2D from tempWidth and tempHeight. After that I dispose that www, but the problem is the memory won’t free.
At the second load, it suppose to call the www.texture again, so the memory is increasing again and still it won’t free after dispose.
I don’t get this problem when I remark line 5-7 and use exact tempWidth and tempHeight (says 1024x768 manually). I don’t know it’s bug or maybe I got wrong using this kind of things.
I recently faced the same problem. I was creating an scrollable image gallery in Unity, where images are downloaded in real-time from the a web server. On iOS devices I was reaching very fast the memory limit (with consequent app crash). That was caused due to a very big number of www objects leaved in the memory and never deleted or released. Also a very big number of Texture2D objects leaked.
The main misunderstanding is considering a Texture2D like a variable of an object that self-contained the image information. Texture2D only point to a referenced asset so we need to create one before assigning to our GUI object. Without creating one we will overwrite the referenced asset in our project, leading to very crazy behaviour.
So this the code that works for me on iOS and just use the minimum of memory
public void DownloadImage(string url)
{
StartCoroutine(coDownloadImage(url));
}
IEnumerator coDownloadImage(string imageUrl)
{
WWW www = new WWW( imageUrl );
yield return www;
thumbnail.mainTexture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(thumbnail.mainTexture as Texture2D);
www.Dispose();
www = null;
}
A brief comment:
You receive a url you will download the image from.
Start the coroutine that manage the download
Create the www object in the local scope
yield waiting the download to complete
Create a new Texture2D object/asset with parameters and assign to your final object mainTexture or what do you want
Use the “www.LoadImageIntoTexture()” function to COPY the image inside the created asset (this is the fundamental part)
Dispose and set to null the www object to prevent memory leaking or orphans
Hope it helps who will face the same problem.
An interesting lecture is this website where they implement a WebImageCache system to avoid re-downloading the same image many times.
If you want to use WWW object for downloading texture.
You SHOULD NEVER USE popoerty WWW.texture. I think, that in this moment WWW instance, creates internal texture (something like new Texture2D()), that is returned to you.
However, this internal texture is not properly disposed and will remain in memory.
Not even WWW.Dispose() will work correctly.
You have to always use only WWW.LoadImageIntoTexture .
And be careful not to use .texture property even for testing if WWW carries the texture or to gain dimensions or any other information about texture!!!
In theory, you can use WWW.texture property. But once you call it, you are responsible, to calling Texture2D.Dispose(wwwTexture) on returnted result. Only then might be created texture properly removed from memory.