Author Topic: Unloading NGUI atlases?  (Read 16869 times)

atcampbell

  • Guest
Unloading NGUI atlases?
« on: March 06, 2013, 12:39:23 PM »
I'm working on a Unity/NGUI application that supports devices with various resolutions. We've created atlases of several resolutions (Standard Def, High Def, etc.) and all of our user interface sprites use a reference atlas. The reference atlas is initially set to use a Standard Def atlas as its replacement. When the app starts, it detects the device's resolution, loads a higher-resolution atlas if necessary, and then switches the reference atlas to use the new atlas as its replacement. All of this works fine, for the most part...

However, we've noticed that the game does not unload the original Standard Def atlas (and its accompanying large texture) after switching to the new higher-definition atlas. We'd like to be able to reclaim this memory.

We tried several ways to unload the atlas before posting to the forum. Here is what we tried, all of which didn't work:
 * Calling Resources.UnloadUnusedAssets()
 * Calling sprite.MarkAsChanged() on all sprites, and then calling Resources.UnloadUnusedAssets()
 * Calling Resources.Unload() on the atlas's texture and material

In conclusion, is there some known way to unload an NGUI asset?

Thanks for your time.

A. T. Campbell, III

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Unloading NGUI atlases?
« Reply #1 on: March 06, 2013, 01:53:30 PM »
The easiest way is to not have the standard definition referenced in the first place. You can just delete the reference from the atlas before building (and of course load it if needed). And it should be fine.

I'm not entirely sure how you can unload it though. You could consider calling Destroy on the mainTexture before you replace it and then Resources.UnloadAllAssets, but that might crash it.

ewhittom

  • Guest
Re: Unloading NGUI atlases?
« Reply #2 on: June 03, 2013, 11:51:35 AM »
Hi,

We're having the same issue here, and the suggested approach does not work in our case. Anyone managed to get this to work?

Thanks,
-Etienne.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unloading NGUI atlases?
« Reply #3 on: June 03, 2013, 10:04:37 PM »
Before unloading anything you have to make sure that it's not referenced by anything first. NGUI creates hidden geometry (that you can reveal by selecting geometry debugging on your panels), and this hidden geometry is what references textures. If you change the atlas reference, the change may not take hold until the following frame -- so your Unload call done in the same frame won't work. You need to delay it, or force-refresh all your panels.

metagod

  • Guest
Re: Unloading NGUI atlases?
« Reply #4 on: August 28, 2013, 04:58:48 AM »
I am doing something similar but via editor scripts, the problem is same though, some of the objects in scene are still somehow referencing to old material, however they are showing the correct atlas in the Inspector window, this is a major issue as unnecessary textures are being loaded in memory. Please advise. 

wizardsmoke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 5
  • Posts: 40
    • View Profile
Re: Unloading NGUI atlases?
« Reply #5 on: September 04, 2013, 07:05:05 PM »
I am experiencing the same issue that metagod described.

You mentioned "force-refreshing" panels.  How do you do that?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile

wizardsmoke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 5
  • Posts: 40
    • View Profile
Re: Unloading NGUI atlases?
« Reply #7 on: September 05, 2013, 11:23:36 AM »
Ah, thanks Aren.

After refreshing all of the panels, it seems like there is still a reference to the old UIAtlas because it is being included in the asset bundle built from the level.  This is how I am refreshing the panels:


  1.         [MenuItem("NGUI/Refresh UIPanels/This Scene")]
  2.         public static void RefreshPanelsInThisScene()
  3.         {
  4.                 Debug.Log("Refreshing all UIPanels in this scene...");
  5.                
  6.                 foreach (UIPanel panel in GameObject.FindObjectsOfType(typeof(UIPanel)))
  7.                 {
  8.                         Debug.Log("Refreshing panel: " + panel.name);
  9.                         panel.Refresh();
  10.                 }
  11.                
  12.                 Debug.Log("Saving Scene.");
  13.                 EditorApplication.SaveScene();
  14.         }
  15.  


Do you still think that this might be a Unity question rather than NGUI?

Thanks for all your help.

wizardsmoke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 5
  • Posts: 40
    • View Profile
Re: Unloading NGUI atlases?
« Reply #8 on: September 05, 2013, 04:49:49 PM »
I didn't really solve this problem, but I was able to work around it by temporarily deleting the atlas before building my asset bundles, I then retrieved the deleted atlas by discarding my changes to it in asset server.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unloading NGUI atlases?
« Reply #9 on: September 05, 2013, 08:10:11 PM »
Asset Server?

Do not use it.

It's bad.

Bad, bad bad bad bad! It's a buggy piece of shit that breaks if you look at it wrong. Use a proper versioning system. Unity has built-in support for perforce.

sisso

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 46
    • View Profile
Re: Unloading NGUI atlases?
« Reply #10 on: September 06, 2013, 08:30:38 AM »
@wizardsmoke, what version of ngui are you using? 2.6.5 have some stuff that could help.

Double check if none of your widgets are directly referencing the HD/SD atlas. Half of time debuging this type of stuff, in the end I found some widget inside a prefab being referenced. Before I build now I check every scene and prefab and cause a error if its happens.

PS: I didn't know about UIPanel.Refresh, good to know.