LoginSignup
2
2

More than 5 years have passed since last update.

PlayMakerでAnimatorを使っていても特定のAnimationの終了通知を受け取るカスタムアクション

Posted at

PlayMakerのAnimator系アクション

実はAnimator系はあまり充実してない。
Animationは揃ってる。Play AnimationアクションならAnimationを再生開始しつつ終了通知を受け取るイベントを設定できるが、今時UnityでAnimation単位で制御しない。MecanimだのAnimatorだのを使う。

EcosystemでもAnimatorで一件もヒットしない。なんたることか。

で、アニメはAnimatorで変数制御したいけど特定のAnimationの終了をイベントで受け取って遷移したいというときのためのカスタムアクション。

AnimationFinishedEvent.cs
using UnityEngine;
using HutongGames.PlayMaker;
namespace HutongGames.PlayMaker.Actions {
    [ActionCategory(ActionCategory.Animation)]
    [Tooltip("Observe an animation finished and send the event.")]
    public class AnimationFinishedEvent : FsmStateAction {

        [RequiredField]
        [CheckForComponent(typeof(UnityEngine.Animator))]
        [Tooltip("The GameObject to be observed.")]
        public FsmOwnerDefault gameObject;

        [RequiredField]
        [Tooltip("Send this event when animation finished.")]
        public FsmEvent sendEvent;

        [RequiredField]
        [Tooltip("The name of the animation to be observed.")]
        public FsmString animationName;

        private Animator animator;

        public override void Reset() {
            gameObject = null;
            sendEvent = null;
            animationName = null;
        }

        // Code that runs on entering the state.
        public override void OnEnter() {
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
            animator = go.GetComponent<Animator>();
        }

        // Code that runs every frame.
        public override void OnUpdate() {
            var a = animator.GetCurrentAnimatorStateInfo(0);
            if (a.IsName(animationName.Value) && a.normalizedTime >= 1f) {
                Fsm.Event(sendEvent);
            }
        }
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2