Hello Guest

Author Topic: Unload tk2danimator assets  (Read 6310 times)

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Unload tk2danimator assets
« on: August 21, 2014, 07:45:20 pm »
Hi,

I noticed that Resources.UnloadUnusedResources() doesn’t unload the atlas from objects that have a tk2danimator script attack to it. This means that during the entire game session, I will have forever all the atlas that has sprites inside from animations (even if I go from the menu into the game while cleaning resources during loading).

What I’m doing is searching for all tk2danimator in scene and Resources.UnloadAsset() of the texture of the object. While this works, is not very safe and costs extra CPU.

So my question is, how can I clear the texture atlas attached to the tk2danimator object that was already destroyed?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload tk2danimator assets
« Reply #1 on: August 21, 2014, 08:04:53 pm »
UnloadUnusedAssets should work, if all references to your objects are removed. Make sure you dont have references to prefabs, etc.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Unload tk2danimator assets
« Reply #2 on: August 21, 2014, 08:14:57 pm »
It works for all assets except the ones that are animations.

I’m deleting an entire prefab with objects using 5 atlas inside of it (2 atlas are for tk2danimations and 3 are for tk2dsprites). This prefabs manages every single object that is his children.
After deleting I’m doing UnloadUnusedAssets() and 3 atlas are cleaned up (the ones for the tk2dsprites) and 2 are still in memory, and there is no way they are removed, except by cleaning with Resources.UnloadAsset(). Unfortunatly Resources.UnloadAsset is not a very good way to solve this because if I later need to resources.load the prefab, all the atlas that were unload will come all wrong.

Cheers

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload tk2danimator assets
« Reply #3 on: August 22, 2014, 07:50:24 pm »
It should definitely be unloaded if there are no references to it. Unfortunately this part of unity is a black box to everyone, and we wont know for certain why its still loaded. You'll need to start narrowing things down, eg. looking for loaded references using Resources.FindObjectsOfTypeAll, etc.... Perhaps something is still loaded, or still has a reference due to not being garbage collected yet, etc.

You are testing this on a built version, and not the editor, correct?

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Unload tk2danimator assets
« Reply #4 on: January 16, 2015, 03:22:56 pm »
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#new

This 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

Code: [Select]
       
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:

Code: [Select]
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:

Code: [Select]
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?
« Last Edit: January 16, 2015, 03:24:30 pm by McTomas »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload tk2danimator assets
« Reply #5 on: January 16, 2015, 04:39:36 pm »
Its not 2D Toolkit that isn't unloading it, its Unity thats reponsible for unloading things there - 2D Toolkit just relies on Unitys memory management. Unity isn't meant to keep references to private vars, no idea why it isn't doing what its supposed to here. At least you have found a solution here.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Unload tk2danimator assets
« Reply #6 on: January 16, 2015, 05:09:33 pm »
Ok, the strange thing is that the only assets that it doesn’t unload are the materials and textures asssociated with tk2dspriteanimations, which are the biggest assets in 2D games