Howdy!
Just upgraded our code base to 2.0 and wanted to get a bit of clarity on best practices around using UnloadTextures
From what I'm seeing, UnloadTextures for a sprite collection loops over the Texture2Ds associated with the collection and calls unload on them. Which is awesome, and I've confirmed in the memory profiler that the memory associated with that atlas is returned.
However, when I later attempt to create a new Tk2DSprite (one from a prefab) that references a sprite in the atlas, I'm seeing the sprite rendered as corrupted.
I think this is because Unity will not Automatically reload the Texture2D for you, and requires you to do another RSC.Load() call to fetch the texture again. I've made some slight tweaks to the SpriteCollection export, process - wondered if you'd agree they're necessary to support Reuse of texture atlases that have been unloaded.
- Each sprite collection data now has a new field, string textureGuids[]
- As part of the atlas creation building in the editor, the textureGuids[] are populated with Guids that reference their asset path and a pointer is added to the tk2d index via MakeLoadableAsset.
- When a Texture atlas has its textures Unloaded, the spriteCollection is marked as being needing to have its atlases reloaded via a static collection that lives in tk2dSpriteCollectionData, indexed by spriteCollectionGuid
- When a tk2dSprite is awake()'ned, the sprite tells the spriteCollectionData to rebuild itself if its dirty
- A spriteCollectionData rebuilds itself by looking up all its textures by guid, and populating them into the index-appropriate material main textures
Does this make sense? is there another way to reuse a texture atlas that has been unloaded in this way?
Second question - more broadly, do you think it makes sense to invest in a ref counter autounload with tk2dsprite? E.g. (based on configuration) Automagically eject the texture atlas when the last tk2dsprite referencing that atlas is destroyed()? Then I think we'd wind up with a more intuitive system where the atlas would load/unload only when being used.