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?