Author Topic: How to properly load and unload UIAtlases?  (Read 16828 times)

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
How to properly load and unload UIAtlases?
« on: October 11, 2012, 09:37:24 AM »
When I try to Resources.UnloadAsset on a UIAtlas loaded with Resources.Load I get the error, "UnloadAsset may only be used on individual assets and can not be used on GameObject's / Components or AssetBundles". If I call it on the UIAtlas's Texture it frees the memory but breaks the atlas and loading the Texture back manually doesn't work (this method does not work with the material either). Is there a method for loading/unloading UIAtlases that you know of that works?

Thanks.
« Last Edit: October 11, 2012, 09:42:07 AM by Fordeka »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #1 on: October 11, 2012, 09:34:04 PM »
Call it on the atlases game object, not the atlas itself.

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #2 on: October 12, 2012, 12:26:46 PM »
Hmm, when I try:
  1. GameObject asset = Resources.Load("Atlases/MyAtlas", typeof(GameObject)) as GameObject;
  2. Resources.UnloadAsset(asset);

I still get, "UnloadAsset may only be used on individual assets and can not be used on GameObject's / Components or AssetBundles".

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #3 on: October 13, 2012, 06:27:19 AM »
Another way to do it is

Destroy(asset);
Resources.UnloadUnusedAssets();

Although that's a bit heavier to run, so don't do it mid-game, where your reactions are needed - do it in the menu at a transition or something like that.

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #4 on: October 14, 2012, 06:52:00 PM »
When I do that, it destroys the actual UIAtlas prefab (or the UIAtlas component itself if you target that with the destroy) from the project completely. If you meant Destroy the UISprites that use the atlas I want unloaded from memory, I already tried that and then called Resources.UnloadUnusedAssets but it did not result in a release of memory.

The only way I have successfully been able to release memory used by atlases is change the scene or unload the textures used by the atlases, but that makes them unusable until I restart the game.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #5 on: October 15, 2012, 06:39:46 AM »
If simply calling Resources.UnloadUnusedAssets() doesn't release memory for you, then your atlas and its texture is still being used somewhere. I can't really suggest much beyond that.

hvilela

  • Guest
Re: How to properly load and unload UIAtlases?
« Reply #6 on: November 07, 2012, 02:00:21 PM »
Have you found a solution to this problem? I have the same issue, how to unload an atlas loaded by "Resources.Load"?

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: How to properly load and unload UIAtlases?
« Reply #7 on: November 09, 2012, 04:20:33 AM »
I think it's working for me like that :

atlasMaterial is a reference to the material of my atlas.

To unload :
  1. Resources.UnloadAsset(atlasMaterial.mainTexture);
  2. atlasMaterial.mainTexture = null;

To load :
  1. atlasMaterial.mainTexture = (Texture2D)Resources.Load(path, typeof(Texture2D));