Ok I’ve taken further look into this problem after reading the bug fix ->
http://2dtoolkit.com/forum/index.php/topic,4738.0/prev_next,prev.html#newThis correction still doesn’t solve the unloading problem but allowed me to find it.
So basically it seems that all objects with tk2dspriteanimator attach to them, won’t be unloaded from memory after a scene switch with
Application.LoadLevel((int) ESCENE.LOADING);
System.GC.Collect();
Resources.UnloadUnusedAssets();
if the objects played in the previous scene where attached into a public/protected/private variable on any object. It is only loaded if the variable referencing the object with tk2dspriteanimator was only temporary declared inside a method.
To give a better example:
public class A : Monobehaviour
{
public GameObject player;
}
public class Player : Monobehaviour
{
private tk2dSpriteAnimator _animator;
}
public class Manager : Monobehaviour
{
void Start()
{
DonDestroyOnLoad(gameobject);
}
public void LoadLevel(int level)
{
Application.LoadLevel(level);
System.GC.Collect();
Resources.UnloadUnusedAssets();
}
With the player variable from the A object referenced in the inspector to the Player GameObject, when switching scenes, all the atlas from the Player _animator animations won’t be unloaded, and I’ll have garbage in my memory that won’t be needed the next scene (I’m 100% sure that there is no need for this atlas because there are no objects using them after carefully comparing Resources.FindObjectsOfTypeAll(typeof(Material)) and Resources.FindObjectsOfTypeAll(typeof(Renderer)).
So I went deeper into it and found out if I switch the A class method to the following it unloads correctly:
public class A : Monobehaviour
{
public GameObject player;
void OnDestroy()
{
player = null;
}
}
After this working I thought that I understand it wrong and I had the NULL every public/protected/private variable in my game. It was a pain but I went for it. But just now I noticed that the only objects that aren’t unloaded are the atlas from my animations and not from static not animated objects.
So my question is, why doesn’t 2D Toolkit unload the textures and materials associated with the previous scene played animations? Is it a bug, or is it intended?