I'm looking for an efficient way to create and use simple sprite animations in Unity.

My situation is the following: I have lots of entities (thousands) which need to be animated. These entities have the same (identical) animation running, which involves just sprite swapping. The animation loops endlessly. The following gif represents an example of what I need. In this example, a single animated object is placed on each tile.

Lots of animated objects

Because of the number of objects, I would prefer to not have an animator-animation combo on each, and I would prefer not to have a MonoBehaviour on each. The overhead from both of these solutions will be way more than the game could handle. The required effect requires just a looping sprite-swapping animation, which can simultaneously run on thousands of objects. The gif above was made with animator-animation combos on each object.

The features offered by Unity's Animator component are great, however way beyond what I need here and thus the overhead is not worth it.

Optimisations I am aware of:

  • Disabling animators when zoomed out.
  • Disabling animators on objects which are out of view.
  • Merging multiple objects into a single one which spans multiple tiles (does not apply due to nature of game)
  • Writing my own sprite swapping animation component (not inherited from MonoBehaviour, similar in idea to this code), placing it on each item. Then creating an AnimationController object (inherited from MonoBehaviour), which updates all instances of my sprite swapping component at regular intervals. This is the solution I will likely go for if nothing else comes up, however involves hand-crafting animations, delays between sprites and such, which will be time consuming.

I feel as though others have surely come across this problem before, so I'm wondering how you solved it. Please also let me know if using Unity's built-in animator-animation system is in fact less resource intensive in this situation than I think.

Thank you!

  • \$\begingroup\$ When you say that the Animator component has too much overhwead, what is it you're seeing? Is the game slowing down? If not you may be optimising too early and could just, as you say, disable the animation when zoomed out, rather than writing your own. If you are seeing a slow down, then the sprite swapping solution seems sound, but I'd advise against making work for yourself before you need to :-) \$\endgroup\$
    – hobnob
    Aug 17, 2016 at 8:29
  • \$\begingroup\$ why... so many fire things. but yeah, what problem are you seeing? \$\endgroup\$
    – Ryan white
    Aug 17, 2016 at 14:44
  • \$\begingroup\$ I've noticed a significant drop in frame rate with 100k sprites (all in view) with an animator-animation combo, vs. without. The difference is 40 FPS with just a sprite renderer vs 11 FPS with a sprite renderer + animator. In terms of premature optimisation, the tile system is the end product, so getting this to work well is a necessity. \$\endgroup\$
    – Liam Lime
    Aug 17, 2016 at 19:49
  • 2
    \$\begingroup\$ Are you still looking for a solution to this? My inclination would be to put all the frames you need into a single flipbook texture, then have the shader automatically advance the frame offset as a function of time. Then you don't need a script to manage the timelines, the animation will just play automatically as part of the rendering pipeline. \$\endgroup\$
    – DMGregory
    Sep 6, 2016 at 0:37


You must log in to answer this question.

Browse other questions tagged .