Unity StateMachineBehaviour
在unity animator中单个Animator Clip中点击Add Behaviour增加当执行该动画时的一些状态代码,请看如下
创建完之后基本代码结构如下:(如果想修改默认代码结构,请看示例:教程示例)
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Hero_walk : StateMachineBehaviour
6 {
7 // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
8 //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
9 //{
10 //
11 //}
12
13 // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
14 //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
15 //{
16 //
17 //}
18
19 // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
20 //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
21 //{
22 //
23 //}
24
25 // OnStateMove is called right after Animator.OnAnimatorMove()
26 //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
27 //{
28 // // Implement code that processes and affects root motion
29 //}
30
31 // OnStateIK is called right after Animator.OnAnimatorIK()
32 //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
33 //{
34 // // Implement code that sets up animation IK (inverse kinematics)
35 //}
36 }
我们在来看一下StateMachineBehaviour元数据
1 #region 程序集 UnityEngine.AnimationModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
2 // D:\WorkTools\Unity\2019.4.16f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.AnimationModule.dll
3 #endregion
4
5 using UnityEngine.Animations;
6 using UnityEngine.Scripting;
7
8 namespace UnityEngine
9 {
10 //
11 // 摘要:
12 // StateMachineBehaviour is a component that can be added to a state machine state.
13 // It's the base class every script on a state derives from.
14 [RequiredByNativeCode]
15 public abstract class StateMachineBehaviour : ScriptableObject
16 {
17 protected StateMachineBehaviour();
18
19 public virtual void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
20 public virtual void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
21 public virtual void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
22 public virtual void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
23 public virtual void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
24 public virtual void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
25 //
26 // 摘要:
27 // Called on the first Update frame when making a transition to a state machine.
28 // This is not called when making a transition into a state machine sub-state.
29 //
30 // 参数:
31 // animator:
32 // The Animator playing this state machine.
33 //
34 // stateMachinePathHash:
35 // The full path hash for this state machine.
36 public virtual void OnStateMachineEnter(Animator animator, int stateMachinePathHash);
37 public virtual void OnStateMachineEnter(Animator animator, int stateMachinePathHash, AnimatorControllerPlayable controller);
38 //
39 // 摘要:
40 // Called on the last Update frame when making a transition out of a StateMachine.
41 // This is not called when making a transition into a StateMachine sub-state.
42 //
43 // 参数:
44 // animator:
45 // The Animator playing this state machine.
46 //
47 // stateMachinePathHash:
48 // The full path hash for this state machine.
49 public virtual void OnStateMachineExit(Animator animator, int stateMachinePathHash);
50 public virtual void OnStateMachineExit(Animator animator, int stateMachinePathHash, AnimatorControllerPlayable controller);
51 public virtual void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
52 public virtual void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
53 public virtual void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
54 public virtual void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
55 }
56 }
是不是很方便呢,今天刚好找到了这个东西