Here is code for an isolated problem, see the last lines with TODO and comments. I would like to hear your thoughts on this?
using Spine;
using Spine.Unity;
using UnityEngine;
public class AnimationTest : MonoBehaviour
{
private const int MainTrack = 0;
private const int HeadTrack = 1;
[SerializeField] private SkeletonAnimation skeletonAnimation;
[SerializeField] private AnimationReferenceAsset idleAnimation; // idle with keyed bone
[SerializeField] private AnimationReferenceAsset walkAnimation; // walk without keyed bone
[SerializeField] private AnimationReferenceAsset headAnimation; // head animation without keyed bone
private void Start()
{
// Start idle
skeletonAnimation.AnimationState.SetAnimation(MainTrack, idleAnimation, true);
// Watch animations start
skeletonAnimation.AnimationState.Start += AnimationStateOnStart;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
// Start walking
// AnimationStateOnStart will be fire and head animation set
var mainTrackEntry = skeletonAnimation.AnimationState.SetAnimation(MainTrack, walkAnimation, true);
mainTrackEntry.MixDuration = 0;
}
if (Input.GetKeyDown(KeyCode.U))
{
// Back to idle
skeletonAnimation.AnimationState.SetAnimation(MainTrack, idleAnimation, true);
skeletonAnimation.AnimationState.SetEmptyAnimation(HeadTrack, 0);
}
}
private void AnimationStateOnStart(TrackEntry trackEntry)
{
// Checking if animation that needs independent head (like walk) started
if (trackEntry.TrackIndex == MainTrack && trackEntry.Animation == walkAnimation.Animation)
{
var headTrackEntry = skeletonAnimation.AnimationState.SetAnimation(HeadTrack, headAnimation, true);
headTrackEntry.MixDuration = 0;
skeletonAnimation.Update(0); // TODO: This is causing the problem!
// When Update(0) is called, the bone stays in idleAnimation position where it was interrupted
// When Update(0) is not called, the bone mix to setup position as intended
// This behavior changed after spine runtime update
// Update(0) is here to prevent missing heads for one frame in some specific cases, but not sure if should be needed?
}
}
}