- Home /
Non-resource texture scope
Hi all,
I'm loading a texture from disk (not in resources) using the www method. It loads fine and I store it in a persistent 'singleton' class. The problem is when I then change scene, the texture disappears as if its been destroyed. The only thing I can think is that maybe manually loaded textures get purged when the scene changes, although I'm holding a persistent ref to it?!
Using c# scripting. Loading code in persistent class:
WWW w = new WWW(picName);
while (!w.isDone)
;
if (w.error == null)
{
mImage = w.texture; // Hopefully copies texture...
}
w.Dispose();
Any ideas on this?
Answer by DaveA · Jan 15, 2011 at 12:10 AM
Loading a new scene will dump things like this. How are you holding it across scene loads? See this thread: http://answers.unity3d.com/questions/841/scene-connecting
Yeah I store the texture in a singleton class - the other data in there persists across scenes, just not this texture!
Hmm. I wonder if Singletons are best at 'primitive' data types, rather than allocated ones. Did you look into DontDestroyOnLoad ?
I did have the object as a DontDestroyOnLoad before, but the singleton approach seems much neater (and better for testing!).
Perhaps the assignment to mImage is just a reference, and the Dispose is really the culprit. What if you didn't call Dispose?
Same behaviour. The www docs say "Each invocation of texture property allocates a new Texture2D"
Your answer