• RuntimesUnity
  • Is it right to use multiple skeletons in an animation?

I have an object that needs to have several different animations. Each animation has different bones.
For example - this is a player. Its front view, side view, back view. I need 3 different skeletons.
Or an icon (example for simplicity) that should change images - sword picture, shield picture. And these animations will also have their own bones.

I have 2 options to solve this problem.
1 - skins. To each skin I connect the bones it needs. It looks strange, but it is also an option.
2 Multiple skeletons. I create multiple skeletons and switch between them.
Here is an example of working code

// Assets
[SerializeField] private SkeletonDataAsset swordIconAnimationSkeleton;
[SerializeField] private SkeletonDataAsset shieldIconAnimationSkeleton;
[SerializeField] private AnimationReferenceAsset ShowShieldIconAnimation;
[SerializeField] private AnimationReferenceAsset ShowSwordIconAnimation;

private void Awake() 
{
  skeletonAnimation = GetComponent < SkeletonAnimation > ();
}

public void ShowSwordIcon() 
{
  SetSkeletonData(swordIconAnimationSkeleton);
  SetAnimation(ShowSwordIconAnimation, false, 1 f);
}

public void ShowShieldIcon() 
{
  SetSkeletonData(shieldIconAnimationSkeleton);
  SetAnimation(ShowShieldIconAnimation, false, 1 f);
}

// Set skeleton
private void SetSkeletonData(SkeletonDataAsset skeletonDataAsset) 
{
  if (skeletonAnimation.SkeletonDataAsset == skeletonDataAsset) return;
  skeletonAnimation.skeletonDataAsset = skeletonDataAsset;
  skeletonAnimation.Initialize(true);
}

// Play animations
private void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale) 
{
  currentTrackEntry = skeletonAnimation.state.SetAnimation(0, animation, loop);
  currentTrackEntry.TimeScale = timeScale;
  currentAnimation = animation;
  currentAnimationName = animation.name;
}

Is it correct to use it this way and switch between skeletons?
How will this affect performance if I need to switch a skeleton quickly? Fast icon change, fast player skeleton change?

Related Discussions
...

Using multiple skeletons for different views or animations, such as front, side, and back views of a player, or different icons like a sword and shield, is a valid approach in Spine and Unity. This method allows you to manage different sets of bones and animations independently, which can be beneficial when the animations or bone structures are significantly different. Switching between skeletons by changing the SkeletonDataAsset and reinitializing the SkeletonAnimation is a common practice, but it can be performance-intensive if done frequently or rapidly, as it involves reloading and reinitializing the skeleton data.

To mitigate performance issues, consider using separate SkeletonAnimation GameObjects for each skeleton and enabling/disabling them as needed, rather than switching the SkeletonDataAsset on a single SkeletonAnimation. This approach initializes each skeleton only once and can improve performance by avoiding repeated reinitialization.

@Elvanov In addition to what Spinebot said, please also note that if you are using a single skeleton, you could blend more smoothly between different orientations via transition animations, if you should need that. You don't have this option when enabling and disabling different GameObjects (or worse, when switching the SkeletonDataAsset an re-initializing single GameObject).

However, in most use-cases it might be the easiest solution to switch between different active GameObjects (SkeletonAnimation objects).

In general you might want to have a look at the following forum thread, mentioning also in related discussions above:
https://esotericsoftware.com/forum/d/24046-specifications-of-using-two-skeletons-for-one-spine-character/2