- Home /
Memory problem with WWW and Texture
Hello,
I'm using WWW for creating textures. Here is the script.
using UnityEngine; using System.Collections; using System;
public class NewBehaviourScript : MonoBehaviour { WWW _www = null; Texture2D _texture = null;
IEnumerator Loading() { Debug.Log("Loading..." + GC.GetTotalMemory(true).ToString()); _www = new WWW("http://saveimg.ru/pictures/23-06-10/bd328a508c9105f873ef4a2a0994f7ed.png"); yield return _www; Debug.Log("Loaded..." + GC.GetTotalMemory(true).ToString()); _texture = _www.texture; //_texture.Compress(false); _www.Dispose(); _www = null; Debug.Log("WWW released..." + GC.GetTotalMemory(true).ToString()); }
void Update() { if (Input.GetKeyDown("a") && _www == null && _texture == null) { Debug.Log("Update StartCoroutine" + GC.GetTotalMemory(true).ToString()); StartCoroutine(Loading()); }
if (Input.GetKeyDown("s") && _www == null && _texture != null)
{
Debug.Log("Update Destroy" + GC.GetTotalMemory(true).ToString());
Destroy(_texture);
_texture = null;
}
}
}
Then I'm trying to get texture from WWW, Unity allocates about 20 Mb of memory (according to internal profiler)
But actually I'm expecting it to allocate 4 Mb. (1000*1000 pixels) * 4 rgba size = 4 000 000 bytes
Is there any way to load texture with out any unexpected memory allocations?
Answer by Gunder · Sep 27, 2010 at 03:21 PM
Try this:
Texture2D tmp = new Texture2D(2,2);
_www.LoadImageIntoTexture(tmp);
Works for me and no mem leakeds.
Answer by peacefulshade · Jun 14, 2013 at 03:26 PM
I had a problem with a dynamically loaded texture, I was calling
GameObject.Destroy(www.texture)
but the getter for the texture in the www class seems to be doing something that prevents the destruction, what ultimatelly worked for me was:
Texture2D texture = www.texture;
//use texture
GameObject.Destroy(texture); www.Dispose();
Hope it helps.