using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class AnimationEventHandler : MonoBehaviour { [HideInInspector] public UnityEvent onAttack; [HideInInspector] public UnityEvent onAttackCheck; [HideInInspector] public UnityEvent onAttackEnd; [HideInInspector] public UnityEvent onHit; Animator animator; readonly List activeClips = new List(); // Start is called before the first frame update void Start() { animator = GetComponent(); } public void RegisterAsActiveClip(AnimationClip clip) { if (!activeClips.Exists(o => o == clip)) activeClips.Add(clip); Debug.Log($"{clip.name} registered"); } public void UnregisterFromActiveClip(AnimationClip clip) { activeClips.Remove(clip); Debug.Log($"{clip.name} unregistered. Remaining clips in transition {activeClips.Count}"); } bool isInTransition { get { AnimationClip currentClip = null; for (int i = 1; i < animator.layerCount; i++) if (animator.GetLayerWeight(i) == 1) { if (animator.IsInTransition(i) == false) return false; currentClip = animator.GetCurrentAnimatorClipInfo(i)[0].clip; } if (activeClips.Exists(o => o == currentClip)) { Debug.Log($"{currentClip.name} is IN TRANSITION"); return true; } return false; } } void Attack(object param) { if (isInTransition == false) onAttack?.Invoke(param); } void Hit(object param) { onHit?.Invoke(param); } void AttackCheck(string param) { if (isInTransition == false) onAttackCheck?.Invoke(param); } void AttackEnd(string param) { Debug.Log("Attack End"); if (isInTransition == false) onAttackEnd?.Invoke(param); } }