• Unity
  • SpineEvent Question

Related Discussions
...

I want to try to trigger a custom animation event
But the HandleEvent event registered in start is not triggered
The following method must be used to register the trigger event later
What could be the cause of this problem? 😢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationEvent : MonoBehaviour
{
    public SkeletonAnimation skeletonAnimation;

public void Start()
{
    skeletonAnimation = GetComponent<SkeletonAnimation>();
    skeletonAnimation.AnimationState.Event += HandleEvent;
    StartCoroutine(InitEvent());
}

IEnumerator InitEvent()
{
    yield return Yielders.WaitForSeconds(1);
    skeletonAnimation.AnimationState.Event += HandleEvent;
}
void HandleEvent(Spine.TrackEntry trackEntry, Spine.Event e)
{
    Debug.Log("Fire_Movent");
    if (e.Data.Name == "Fire_Movent")
    {
        Debug.Log("Fire_Movent");
    }
    Debug.Log("Fire_Movent");
}
}

AnimationState is initialized when SkeletonAnimation.Initialize() is called the first time, or with parameter overwrite = true.
It is typically called for the first time via the base class SkeletonRenderer.Awake.

Problems will occur when calling SkeletonAnimation.Initialize() and overwriting the existing AnimationState with a new one after registering your event at the AnimationState. Since you are registering your event in Start(), Awake() should have been called beforhand already. Are you perhaps calling SkeletonAnimation.Initialize(true) somewhere in your code? You could set breakpoints at your line skeletonAnimation.AnimationState.Event += HandleEvent; and at SkeletonAnimation.cs:Line 173 and check in the debugger if something overwrites your AnimationState and thus discards your registered events.

Harald thanks, it's work! I find the initialized in somewhere else. 😃

Glad to hear you've figured it out, thanks for getting back to us! 🙂