Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animating sprite from disk

Discussion in '2D' started by nia1701, May 11, 2015.

  1. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    For large memory intensive sprite animations it is important to be able to stream them from disk as to avoid every frame taking up memory on mobile devices.

    To do this I have a simple solution I use to load a sprite from resources and then unload that sprite for each frame of an animation.

    Here is a condensed and simplified version of the code I currently use:

    Code (csharp):
    1.  
    2. //ref to sprite renderer
    3. SpriteRenderer spRend = GetComponent<SpriteRenderer>();
    4.  
    5. //hold onto current sprite being used in a sprite renderer.
    6. Sprite sp = spRend.sprite;
    7.  
    8. if(sp != null)
    9. {
    10.     //first unload the texture from memory
    11.     Resources.UnloadAsset(sp.texture);
    12.  
    13.     //now unload the sprite object from memory
    14.     Resources.UnloadAsset(sp);
    15. }
    16.  
    17. //load a new sprite from resources
    18. spRend.sprite = Resources.Load<Sprite>("newSpriteResourcesPath");
    19.  
    Anyone have any thoughts on a better or more efficient way to do this?

    Thanks!
     
    Last edited: May 11, 2015