- Home /
Single Animator for multiple similar sprite sheets
Hey guys,
I have an interesting question that probably has a simple answer I'm not aware of at this point, so any assistance from people more knowledgeable on this subject would be great =)
I'm working on a 2D game that has a character, represented by a UI.Image, with multiple skins (Different colorations of the exact same animations in this case). Now, since it's a sprite, to animate this character I basically made an Animator that controls animations that change the active sprite of the character. The problem is that due to sprite changes being recorded as part of the animation, that means I need to create a separate Animator and animations for each character that are exactly the same and only use each skin's equivalent sprite on each keyframe.
Given that my 2 skins have exactly the same number of frames which are exactly the same in poses on each frame and only differ in minor details, I'm looking for a solution that will let me use a single Animator that I can feed a sprite sheet to as an input parameter and the Animator will just tell the image which sprite in the spritesheet it should use on each keyframe.
Something like a Strategy Design Pattern but for animations, if that makes sense.
Is anyone aware of such a solution or do I need to write this on my own?
Thanks a lot~
Note, I had in $$anonymous$$d the same solution suggested in this post: https://forum.unity3d.com/threads/different-sprites-with-the-same-animation.410769/
However, I'm looking for a solution that's more elegant to our content creators, and was generally wondering if a tool exists for this job already.
Answer by KazenoZ · Jul 28, 2017 at 11:38 PM
I ended up going with a solution similar to this one: https://forum.unity3d.com/threads/different-sprites-with-the-same-animation.410769/
Except I separated the functionality to 2 classes
public class AnimationDispatcher : MonoBehaviour
{
public void SetSpriteId(int i_SpriteId)
{
foreach (AnimationHandler handler in GetComponentsInChildren<AnimationHandler>())
{
handler.SetSpriteId(i_SpriteId);
}
}
}
And
public class AnimationHandler : MonoBehaviour
{
[SerializeField]
private string m_SpriteSheetPath;
private Sprite[] m_Sprites;
private Image m_CurrentSprite;
// Use this for initialization
private void Awake()
{
m_Sprites = Resources.LoadAll<Sprite>(i_SpriteSheetPath);
m_CurrentSprite = GetComponent<Image>();
m_CurrentSprite.sprite = m_Sprites[0];
}
public void SetSpriteId(int i_SpriteId)
{
m_CurrentSprite.sprite = m_Sprites[i_SpriteId];
}
}
Where AnimationDispatcher is placed on the animated object and the SetSpriteId is called by the Animation Event, and AnimationHandler is placed on the specific skins childed to the object holding the AnimationDispatcher. This allows me to forward the sprite change request to the specific active skin.
Note that my skins dynamically change during the game, making it impossible to gather a list of AnimationHandlers in the Awake method of the script, but normally it's a good idea to populate that list once instead of poll for it this often, as it's a performance-problematic call.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What are the best practices for reusing an animation controller? 0 Answers
2DAnimator Freezes first time an animation is played 0 Answers
Animator Controller 2D RPG Best Practices 0 Answers
Animtion looping if joystick is held 0 Answers
Is there a new retargetting system for the animation in 5.5? 1 Answer