Reputation: 111
I'm using this to unload the textures when I no longer need the prefab (is a popup):
UISprite[] widgets = gameObject.GetComponentsInChildren<UISprite>(true);
for (int i = 0, imax = widgets.Length; i < imax; i++)
{
UISprite w = widgets[i];
Debug.Log ("Removing: " + w.gameObject.name);
if (w.mainTexture)
{
Debug.Log ("Removing: " + w.mainTexture.name);
Resources.UnloadAsset(w.mainTexture);
}
}
This is working as I can see, since the Texture2Ds are not showing anymore in the profiler after the unload. But the problem is that when I instantiate the popup again, the sprites are shown like white boxes. So, they are not reloaded when needed as it is said in the documentation: If there are any references from game objects in the scene to the asset and it is being used then Unity will reload the asset from disk as soon as it is accessed.
What I am doing wrong?
Upvotes: 4
Views: 5711
Reputation: 1792
Looks like you are trying to overcomplicate this a little and you are unloading resources from the memory completely. You should simply destroy parent GameObject you want to remove from the scene.
Destroy(GameObject);
http://docs.unity3d.com/ScriptReference/Object.Destroy.html
Unity garbage collector will remove all references to the scene and Unload Unused Resources automatically.
Upvotes: 1