关闭

Unity5.0新特性 StateMachineBehaviours

标签: Unity
166人阅读 评论(0) 收藏 举报
分类:

无意中发现了这个新特性,都怪自己没有仔细看unity的更新文档。

首先贴上StateMachineBehaviour类的代码。


#region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// E:\Unity Folder\Projects\LoadingDemo\Library\UnityAssemblies\UnityEngine.dll
#endregion

using UnityEngine.Experimental.Director;
using UnityEngine.Scripting;

namespace UnityEngine
{
    //
    // 摘要:
    //     ///
    //     StateMachineBehaviour is a component that can be added to a state machine state.
    //     It's the base class every script on a state derives from.
    //     ///
    [RequiredByNativeCodeAttribute]
    public abstract class StateMachineBehaviour : ScriptableObject
    {
        protected StateMachineBehaviour();

        public virtual void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
        public virtual void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
        public virtual void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
        public virtual void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
        public virtual void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
        public virtual void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
        //
        // 摘要:
        //     ///
        //     Called on the first Update frame when making a transition to a StateMachine.
        //     This is not called when making a transition into a StateMachine sub-state.
        //     ///
        //
        // 参数:
        //   animator:
        //     The Animator playing this state machine.
        //
        //   stateMachinePathHash:
        //     The full path hash for this state machine.
        public virtual void OnStateMachineEnter(Animator animator, int stateMachinePathHash);
        public virtual void OnStateMachineEnter(Animator animator, int stateMachinePathHash, AnimatorControllerPlayable controller);
        //
        // 摘要:
        //     ///
        //     Called on the last Update frame when making a transition out of a StateMachine.
        //     This is not called when making a transition into a StateMachine sub-state.
        //     ///
        //
        // 参数:
        //   animator:
        //     The Animator playing this state machine.
        //
        //   stateMachinePathHash:
        //     The full path hash for this state machine.
        public virtual void OnStateMachineExit(Animator animator, int stateMachinePathHash);
        public virtual void OnStateMachineExit(Animator animator, int stateMachinePathHash, AnimatorControllerPlayable controller);
        public virtual void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
        public virtual void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
        public virtual void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
        public virtual void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller);
    }
}

那我们要怎样使用呢?

首先我们要选中AnimatorController中的某一个状态,


OK,图上的SMBExample脚本继承自StateMachineBehaviour类并重写了部分方法,贴上代码


using UnityEngine;
using System.Collections;

public class SMBExample : StateMachineBehaviour
{

    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateEnter(animator, stateInfo, layerIndex);
        Debug.Log("OnStateEnter");
    }

    public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateExit(animator, stateInfo, layerIndex);
        Debug.Log("OnStateExit");
    }

    public override void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateIK(animator, stateInfo, layerIndex);
        Debug.Log("OnStateIK");
    }

    public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateMove(animator, stateInfo, layerIndex);
        Debug.Log("OnStateMove");
    }

    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateUpdate(animator, stateInfo, layerIndex);
        Debug.Log("OnStateUpdate");
    }

    //Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller

    //OnStateMachineEnter
    //OnStateMachineExit

}


随着 MonoBehaviours, StateMachineBehaviour 函数在特定情况下被调用。

  • OnStateEnter 正在played的状态的第一帧被调用。

  • OnStateUpdate  MonoBehaviour Updates 更新后被调用,每一帧animator 是playing 时这个行为的状态。

  • OnStateExit 转换到另一个状态的最后一帧 被调用。

  • OnStateMove 在OnAnimatorMove之前被调用 ,将在MonoBehaviours 调用之前的每个帧的state playing 。当调用 OnStateMove 时,它将停止 MonoBehaviours 调用 OnAnimatorMove。

  • OnStateIK is called after OnAnimatorIK on MonoBehaviours for every frame the while the state is being played. It is important to note that OnStateIK will only be called if the state is on a layer that has an IK pass. By default, layers do not have an IK pass and so this function will not be called. For more information on IK see the information linked below.

  • OnStateMachineEnter 在 animator plays 的内容的一个子状态机的第一帧上被调用。

  • OnStateMachineExit 从一个子状态机过渡的最后一帧上被调用。




其中这些方法中的三个参数含义为:


Animator 动画参数是特定的animator ,是这个状态机行为的引用。例如,这可以用于设置 动画参数的值只能在此状态下,例如,用于blend tree混合树。

AnimatorStateInfo 是状态机的行为是对state 的当前信息。它是相当于writing animator.GetCurrentStateInfo(layerIndex);  这可以涉及该clip剪辑的正常的时间的操作很有用。

LayerIndex 是状态机行为状态的layer 层。例如,0为基底图层,1用于第一个 等等。


参考博客: http://blog.csdn.net/u010019717/article/details/44760683

https://segmentfault.com/a/1190000003739237


更加详细的深入该类请查看第二个网址的文章。

博主只尝试了最简单的方法,还没有更加深入的运用,下次继续编辑这文章吧,哈哈。




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4367次
    • 积分:36
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:11篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论