Hello Guest

Author Topic: Unused atlas textures still loaded after LoadLevel  (Read 21635 times)

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Unused atlas textures still loaded after LoadLevel
« on: May 05, 2012, 04:40:04 am »
we're seeing a problem where atlases for a sprite collection we use in our main menu are carried into our first level.  we can't afford to load those.  they cause the iPad 1 to crash eventually.

how can we dump those or prevent this from happening?  i'm 99.5% sure we have no references to those collections in the first level ( or any subsequent level )

Oh.. and I should mention I went so far as to load an empty scene in-between scenes, wait while Resources.UnloadAllUnusedAssets() did it's thing and then load my new scene additively.   which is an awful way of having to dump all assets.. ? oh and light probes don't load in LoadLevelAdditive.. so I can't use it anyway.

are there persistent hidden tk2d objects?

thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #1 on: May 05, 2012, 07:33:44 am »
There aren't any persistent objects in tk2d. First things first, we need to identify what remains loaded at this point. Can you do something like this to find all loaded objects, and identify the remaining one(s)?

http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,228.msg976.html#msg976

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #2 on: May 05, 2012, 07:53:06 am »
I used something similar to see that the menu atlases were still loaded in the game levels.

-----------------------------------
------- Loaded Textures -----------
-----------------------------------

31. atlas0   1024x1024
32. atlas0   1024x1024
33. atlas0   1024x1024
34. atlas0   1024x1024
35. atlas1   1024x1024
36. atlas2   512x512


the 3 in red ( well at least one of the atlas0's ) is from the menu collection.. definitely atlas 1 and 2 as they are the only textures we have named that.   This snapshot of loaded textures was taken a few seconds after map load and AFTER Resources.UnloadAllUnusedAssets() finished while I waited in a Coroutine checking async to see if it was done. 

I enumerated meshFilters too and then checked the renderers to see if any shared materials were set to atlas2 ( to see what might still have hooks into those textures ) but nothing turned up.  I'll enumerate MeshRenderers and try again.

Update:  enumerating the MeshRenderers and their materials didn't find any matches

enumerating the materials did show:

17. atlas0_material
18. atlas0_material
19. atlas0_material
20. atlas0_material
21. atlas1_material
22. atlas2_material

which are the sprite collection material names

-andrew
« Last Edit: May 05, 2012, 08:32:18 am by mrscary »

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #3 on: May 05, 2012, 08:53:23 am »
All four of the TK2dSpriteCollectionData prefabs are still loaded in the game level

4605. data
4606. data
4607. data
4608. data

so something is still addressing that sprite collection.  I think i'll enumerate the game objects looking for tk2dBaseSprite.. when I find them, I'll look for a tk2dSpriteCollectionData matching the one I'm looking for.  I can add a string to the collection data to identify them more easily ( scratch that, I see the sprite collection name is in there already just hidden :) ).

if you have other suggestions, let me know. thanks

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #4 on: May 05, 2012, 09:28:03 am »
I found the asset referencing that sprite collection

482. SceneMenuItem collection: MenuBgCollection
483. SceneMenuItem/ActLabel
484. SceneMenuItem/SelectionHighlight collection: MenuBgCollection
485. SceneMenuItem/Shadow collection: MenuBgCollection

This is a prefab of four sprites ( one parent, three children ) that I use to instantiate level selection on our main menu.  So there's a UISceneList script that has a reference to this prefab, then creates a bunch of "scene menu items" from that prefab that are displayed.

So, Unity thinks this prefab should stick around for some reason.  I'll look for static references in the morning.  If that isn't successful, I'll have to find another way to do the menus?


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #5 on: May 05, 2012, 09:37:13 am »
So the UISceneList script has a reference to this prefab, but does this still exist at this point? Worst case, you can simply store the reference to the prefab in resources and load it in dynamically.

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #6 on: May 05, 2012, 07:47:37 pm »
Even if I loaded it directly from Resources, wouldn't it still hang around level to level if I can't figure out what's keeping a reference to it?

There's this in my UISceneList class

static private UISceneList   thisList = null;

When I first saw that, I thought "there's the problem".  So I clean it up like this:

   /*
   -----------------------
   OnDestroy()
   -----------------------
   */
   void OnDestroy() {
      // this clears the fracking reference to the scene item prefabs that were still in
      // memory because thisList is static
      itemPrefab = null;
      listItems.Clear();
      if ( thisList == this ) {
         thisList = null;
      }
   }

That didn't fix it.. but maybe that class is never getting destroyed :)  I'll investigate some more.. and probably remove the static reference.

thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #7 on: May 05, 2012, 08:13:39 pm »
Looking at that bit of code, it looks like you intend it to be destroyed at that point anyway - perhaps its worth making it non-static?

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #8 on: May 05, 2012, 09:05:52 pm »
Well I made it non-static and it's still loaded.

Additionally, there are no longer any references to it in the game object listing.

I'm beginning to wonder if this isn't just another Unity bug:  http://forum.unity3d.com/threads/47858-Forcing-texture-unload

... or perhaps it's because instanced materials from multi atlas collections are leaking?

Code: [Select]
protected override void UpdateMaterial()
{
if (renderer.sharedMaterial != collection.spriteDefinitions[spriteId].material)
renderer.material = collection.spriteDefinitions[spriteId].material;
}

Note.. that ^^ is not it.. i just commented that code out and it made no difference.
« Last Edit: May 05, 2012, 09:16:09 pm by mrscary »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #9 on: May 05, 2012, 09:12:12 pm »
So its gone from the game object listing, but its what still remains? Just the texture & material? What about the prefab?
It may well be the same Unity bug if the textures all that remains.
« Last Edit: May 05, 2012, 09:18:10 pm by unikron »

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #10 on: May 05, 2012, 09:20:54 pm »
As far as I can tell, it's just the textures at this point and the materials... which I would guess the materials are keeping the textures loaded.

Sorry.. I re-listed tk2dBaseSprites and the scene menu item prefabs showed up.  I missed those prefabs in the GameObjects list, but verified they are still listed.  All of the Prefabs I use to instantiate gui items are still loaded, even though the objects that instantiate them ( i.e. UISceneList ) are not.

I may try loading these prefabs from Resources as you suggested.  Should they get automatically unloaded on scene changes, or do I have to do something special?

(pseudocode)
GameObject prefab = Resources.Load( prefabNameStr );
for ( int i = 0; i < sceneCount; i++ ) {
   ...instantiate items....
}
Destroy( prefab );  ??  ( i think this will prevent me from loading it again which would happen when I return to the menu )
prefab = null; ??

... or.. just generate the sprites and text procedurally.
« Last Edit: May 05, 2012, 09:53:34 pm by mrscary »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #11 on: May 05, 2012, 09:54:29 pm »
if you Destroy whatever you Instantiate, you should be good. Thats what I do in all my stuff, never had any trouble with it so far. I keep getting these nightmares of Unity messing it all up, have the occasional hunt for the random thing left in, but in all cases so far its been something I've done wrong.

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #12 on: May 05, 2012, 10:52:40 pm »
Well... I thought Unity would destroy what you instantiate on level changes if no references are left behind?   However, I do explicitly destroy the scene list..and the scene items are children of the scene list.  Debug messages in the scene item's OnDestroy() confirm they are being destroyed.  That said, I added explicit code to destroy these items in the scene list OnDestroy

Code: [Select]
/*
-----------------------
OnDestroy()
-----------------------
*/
void OnDestroy() {
Debug.LogWarning( "UI SCENELIST GETTING DESTROYED &&&&&&&&&&&&&&&&&&&&&&&&&");
itemPrefab = null;
for ( int i = 0; i < listItems.Count; i++ ) {
Destroy( listItems[i] );
}
listItems.Clear();
}

This didn't help.     I have to believe it's the fact that I have a prefab for the scene list gui ( scene list is part of it ), and one of the scene list data fields is another prefab for the scene items.   All that's left from the main menu in the game scene is this stupid scene item prefab..  So I'm going to get rid of that prefab and procedurally create the scene items.  If that fixes it, I will create a test project and submit a bug to unity.   If it doesn't.. well  >:(

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #13 on: May 05, 2012, 11:35:34 pm »
What does itemPrefab contain? It probably isn't relevant, but asking just in case. Presumably its bound by dragging an object into the slot?

mrscary

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unused atlas textures still loaded after LoadLevel
« Reply #14 on: May 06, 2012, 12:17:13 am »
Yes, dragged into a GameObject slot..  See email for screenshots of the prefab etc ( easier ).