- Home /
Animation and Sprite-Swapping
Hey everyone. I've combed through the forums and docs, and I've seen many questions similar, but I can't seem to find an efficient way to do this.
So basically, say I have an NPC with a whole heap of animations based on one sprite sheet. Is it possible to use the same animation clips/controller with a different set of sprites?
So far I've uncovered two methods of doing this:
Replace all sprites in the SpriteRenderer component using Resources.Load in LateUpdate() with sprites of the same name. (Courtesy of Unity lectures video).
Use Animation Events to reassign sprites manually through script.
Both of these methods work fine, but the first is very resource-heavy, especially in multiple instances, and the second requires a lot of code on my part (since there will be 50+ animations per NPC), or a large sprite array, which again will be resource-heavy with several instances. Are there any alternate methods to achieve this, preferably without a lot of overhead?
Also, does anyone know if Unity plans to integrate something like this any time soon? Some of the forum posts are from back in 2010.
Answer by rinenproductions · Jan 08, 2020 at 06:45 PM
I had the exact same problem in my own project and I was able to solve it with an editor tool which works like this:
Read the contents of a user-selected Animator Controller asset
Read and store sprite-related keyframe data (original sprites, frame time) from the Animation Clips which are referenced inside the Animator Controller asset
Find original spritesheet texture assets used in those keyframes
Allow user to select the replacements for those original spritesheet texture assets
Generate a Scriptable Object containing all sprite-related keyframe data with equivalent sprites from user-specified sources for each animation state along with the state hash
The generated Scriptable Object asset can then be used to override the sprites set by the Animator component during runtime by checking the current animation hash and picking out a replacement sprite from the Scriptable Object.
This way you are able to reuse the same Animator Clips and even the Animator Controller for any amount of animated objects which differ only in spritesheet sources.
You can find my solution to this problem here: https://assetstore.unity.com/packages/tools/animation/animator-sprite-swap-system-159842
Your answer
![](https://koobas.hobune.stream/wayback/20220612081953im_/https://answers.unity.com/themes/thub/images/avi.jpg)