Hello Guest

Author Topic: Sprite and Memory Management  (Read 4686 times)

Maserat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite and Memory Management
« on: November 17, 2012, 03:30:29 pm »
Currently I have several sprite collections say walk, jump, fire, and dance linked to a animation collection called playeranimations. If the character is walking and I call Play( "jump" ) and then Play( "dance" ), what is still in memory? Is the previous spritesheet still in memory or do I have to release it manually? Are some of the spritesheets preloaded for speed? Just trying to figure out what is going on in the background.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite and Memory Management
« Reply #1 on: November 17, 2012, 06:46:10 pm »
All sprite collections, materials and textures are loaded with the sprite animation if they are used in the scene. They are unloaded when the sprite animation is unloaded, and you call Resources.UnloadUnusedAssets, or when the scene is changed.

phildo77

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Sprite and Memory Management
« Reply #2 on: January 04, 2013, 04:14:02 am »
I'd like a little more clarification on this...
"All sprite collections, materials and textures are loaded with the sprite animation if they are used in the scene."

First of all, I don't change scenes at all.  The entire game is in one Unity "Scene".  Instead I'm loading screens all that have their own atlases EXCEPT for the main character which is used through all screens.  Each screen is destroyed before the next one is instantiated.  Sometimes the main character has a special animation (or more) that is only used in a single screen.  I'd like to put that animation in the SCREEN atlas/collection and not the main character animation to save memory.  Is this possible?  See below for more of a description:

Say I have a SpriteAnimation (Main Character from above) that contains animations using 5 different sprite collections.

I load up my first screen (there are NO scene changes) and I use animations from collections (atlases) 1 and 2 but not collections 3,4 or 5.

Then I close my first screen and open my second (again NO scene changes) and I use animations from collections 1 and 5 but not collections 2,3 or 4.

Third screen 1 and 3 and so on.

If I call Resources.UnloadUnusedAssets() between screen changes, does this keep the unnecessary collection atlases from being loaded into memory for each of my "Screens"?  If not is there another way to do what I describe above?  Or am I going to have to just throw everything for the "Main Character" into one sprite collection?

Happy New Year and Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite and Memory Management
« Reply #3 on: January 04, 2013, 08:09:42 am »
You are at the mercy of Unity unloading and loading things at the appropriate times using the default behaviour. The easiest way to do this is and have full control over everything is - 

1. One or more sprite collections for the base shared sprites. One SpriteAnimation for all these animations.

2. All the "Screen" specific data is in its own sprite collection each. Create a SpriteAnimation specific to each "screen", give it a unique name and put it in a "Resources" directory. Just the SpriteAnimation and nothing else. One or more animations in each of these.

3. When switching to these screens, load the SpriteAnimation using Resources.Load, and create your animated sprite. Creating a sprite is best, as you can simply destroy it to ensure all references are destroyed. Make sure you don't save a reference to the SpriteAnimation. The only one who actually contains a reference to it is the animated sprite.

4. Destroy the sprite and call Resources.UnloadUnusedAssets and you should unload all references.


Note: Creating a new sprite is easier for this reason: When you play an animation, and the animation stops, the material assigned to the sprite will still be the last frame it played. Which in this case, will be the material from the "screen" sprite collection. You have to make sure there are no lingering references before unloading.