出处:http://blog.csdn.net/u010019717

author:孙广东      时间:2015.3.31

(State machine behaviours)状态机的行为在Animator Controller的脚本中是能够附加到动画状态或子状态机。每当你进入一种状态,就能够对其加入各种各样的状态依赖比如播放声音等行为,他们甚至能够独立于动画animation。而用于(logic state machines )逻辑状态机。

要加入(State machine behaviours)状态机行为到状态或子状态机,请单击inspector中的Add Behaviourbutton。

从这里你能够选择从现有存在的 (State machine behaviours)状态机行为,或创建一个新的。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDAxOTcxNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />

新的(state machine behaviours )状态机行为被创建在C#语言中.

全部(state machine behaviours )状态机行为从同一个基类继承。StateMachineBehaviour。

因为他们能够支持继承,假设您希望加入多个类。当然这个功能能够轻易的实现。

有关继承的很多其它信息,请參阅以下链接的信息。

StateMachineBehaviour 函数

(state machine behaviours )状态机行为的核心是 5 个函数:在(animator state )动画状态中自己主动被调用的 和 2个函数 :在一个子状态机中被自己主动调用的。

它们在创建时,加入到新的(state machine behaviours )状态机的行为中 会凝视掉一些超出版本号的这些函数 。这些函数是 OnStateEnter、 OnStateUpdate、 OnStateExit、 OnStateMove 和 OnStateIK。附加功能的子状态机转换(transitions )是 OnStateMachineEnter 和 OnStateMachineExit。

全部的这些函数有三个參数传递给他们: Animator,  AnimatorStateInfo和 layer 索引。

override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}

·  (Animator parameter 动画參数是特定的animator ,是这个状态机行为的引用。比如,这能够用于设置 动画參数的值仅仅能在此状态下,比如,用于blend tree混合树。

·  AnimatorStateInfo 是状态机的行为是对state 的当前信息。

它是相当于writing animator.GetCurrentStateInfo(layerIndex);  这能够涉及该clip剪辑的正常的时间的操作非常实用。

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

随着 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 从一个子状态机过渡的最后一帧上被调用。

使用(State Machine Behaviours)状态机行为的演示样例

考虑了beat’em up风格的游戏 在当你执行特殊动作。你想要播放粒子系统particle systems 和 攻击的你死我活。此脚本能够用含有特殊的move’s 动画的动画状态。

using UnityEngine;

public class SpecialAttackParticlesSmb : StateMachineBehaviour
{
public GameObject particles; // Prefab of the particle system to play in the state.
public AvatarIKGoal attackLimb; // The limb that the particles should follow. private Transform particlesTransform; // Reference to the instantiated prefab's transform.
private ParticleSystem particleSystem; // Reference to the instantiated prefab's particle system. // This will be called when the animator first transitions to this state.
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// If the particle system already exists then exit the function.
if(particlesTransform != null)
return; // Otherwise instantiate the particles and set up references to their components.
GameObject particlesInstance = Instantiate(particles);
particlesTransform = particlesInstance.transform;
particleSystem = particlesInstance.GetComponent <ParticleSystem> ();
} // This will be called once the animator has transitioned out of the state.
override public void OnStateExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// When leaving the special move state, stop the particles.
particleSystem.Stop();
} // This will be called every frame whilst in the state.
override public void OnStateIK (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// OnStateExit may be called before the last OnStateIK so we need to check the particles haven't been destroyed.
if (particleSystem == null || particlesTransform == null)
return; // Find the position and rotation of the limb the particles should follow.
Vector3 limbPosition = animator.GetIKPosition(attackLimb);
Quaternion limbRotation = animator.GetIKRotation (attackLimb); // Set the particle's position and rotation based on that limb.
particlesTransform.position = limbPosition;
particlesTransform.rotation = limbRotation; // If the particle system isn't playing, play it.
if(!particleSystem.isPlaying)
particleSystem.Play();
}
}

MonoBehaviours 和 StateMachineBehaviours 之间的通信

要正确理解 MonoBehaviours 和 StateMachineBehaviours 之间的差别是有必要了解一下,assets 资产和scene objects场景对象之间的差别。

场景对象仅仅存在于一个单一的场景。

这些包含GameObjects 游戏对象和它们的components 组件以及prefabs预置的实例。

Assets 存在project 中,能够在不论什么场景中引用。

这些包含动Animator Controllers, prefab assets 和 StateMachineBehviours。因为scene 与一个特定的对象可能不会loaded载入。asset 资产不能引用一个场景对象。因为asset 资产总是存在于project中,场景objects 能够引用assets。

请记住 state machine behaviours 是 Assets,它们不能储存scene objects的引用。为了在state machine behaviour 状态机行为中引用一个scene object ,该引用的对象必须能被 找到或传递到状态机行为。举个样例:

override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.Find(“Player”);
}

(State machine behaviours)状态机行为他们的创建方式不像MonoBehaviours 。

当MonoBehaviours 实例被创建并被加入到一个游戏对象上因此成为场景对象。StateMachineBehaviour 类派生自 ScriptableObject。因此,状态机行为是 assets,不是场景对象。    这意味着状态机行为要想存在在一个场景中,状态机行为在执行时自己主动创建实例 ,在Animator’s 的内部Awake 调用。这意味着在 MonoBehaviour 的 Awake 函数期间 找到 对它们的引用是不建议。因为它将产生不可预知的结果。

在MonoBehaviour中 要获得对 StateMachineBehaviour 的引用 ,能够使用不论什么 animator.GetBehaviour<>() 或者 animator.GetBehaviours<>().  GetComponent<>() and GetComponents<>().的功能相似 。GetBehaviour 将在animator上找到的第一个实例 返回指定的 StateMachineBehaviour 。GetBehaviours 将返回找到的指定类型的全部 StateMachineBehaviours 的数组。因为不能保证状态机行为已在Awake中实例化,应在Start 函数值红 调用这些函数。

这里是单一的 StateMachineBehaviour 和 MonoBehaviour 之间的查找 引用一个短的样例。

using UnityEngine;

public class ExampleMonoBehaviour : MonoBehaviour
{
private Animator animator; // Reference to the Animator component on this gameobject.
private ExampleStateMachineBehaviour exampleSmb; // Reference to a single StateMachineBehaviour. void Awake ()
{
// Find a reference to the Animator component in Awake since it exists in the scene.
animator = GetComponent <Animator> ();
} void Start ()
{
// Find a reference to the ExampleStateMachineBehaviour in Start since it might not exist yet in Awake.
exampleSmb = animator.GetBehaviour <ExampleStateMachineBehaviour> (); // Set the StateMachineBehaviour's reference to an ExampleMonoBehaviour to this.
exampleSmb.exampleMb = this;
}
}

using UnityEngine;

public class ExampleStateMachineBehaviour : StateMachineBehaviour
{
public ExampleMonoBehaviour exampleMb;
}




(三)Unity5.0新特性------动画的StateMachineBehaviours的更多相关文章

  1. (六)Unity5.0新特性------新动画功能

     unity 5.0 中的新动画功能 这里是你能够期待的新动画功能高速概述 ! State Machine Behaviours状态机行为 在Unity 5 中,你会能够将StateMachine ...

  2. (十)Unity5.0新特性------新UI系统实战

    原文 Unity New GUI Tutorial – Part 1 Unity New GUI Tutorial- Part 2 Unity New GUI Tutorial – Part 3 大家 ...

  3. 【mysql】mysq8.0新特性

    一.MySQL8.0简介   mysql8.0现在已经发布,2016-09-12第一个DM(development milestone)版本8.0.0发布.新的版本带来很多新功能和新特性,对性能也得到 ...

  4. C# 8.0的三个令人兴奋的新特性

    C# 语言是在2000发布的,至今已正式发布了7个版本,每个版本都包含了许多令人兴奋的新特性和功能更新.同时,C# 每个版本的发布都与同时期的 Visual Studio 以及 .NET 运行时版本高 ...

  5. atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性

    atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性   1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...

  6. C# 7.0 新特性3: 模式匹配

    本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...

  7. C# 7.0 新特性4: 返回引用

    本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  8. C#发展历程以及C#6.0新特性

    一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...

  9. [转]Servlet 3.0 新特性详解

    原文地址:http://blog.csdn.net/xiazdong/article/details/7208316 Servlet 3.0 新特性概览 1.Servlet.Filter.Listen ...

随机推荐

  1. 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  2. 借Stunnel工具保护E-mail服务器

    借Stunnel工具保护E-mail服务器 650) this.width=650;" onclick='window.open("http://blog.51cto.com/vi ...

  3. 洛谷 P1032 字符变换

    洛谷 P1032 字符变换 题目描述 已知有两个字串 A,B 及一组字串变换的规则(至多 6 个规则): A1​ -> B1​ A2​ -> B2​ 规则的含义为:在 A 中的子串 A1​ ...

  4. Shiro学习总结(4)——Shrio登陆验证实例详细解读

    最终效果如下: 工程整体的目录如下: Java代码如下: 配置文件如下: 页面资源如下: 好了,下面来简单说下过程吧! 准备工作: 先建表: [sql] view plain copy drop ta ...

  5. spring @configuration使用

    http://yaobenzhang.blog.163.com/blog/static/2143951132014811105138824/

  6. 使用dotcloud免费ssh

    使用dotcloud免费ssh https://www.dotcloud.com一个项目在线托管网站,注册后可以免费托管两个项目. 注册帐号,在ubuntu中执行下面命令,安装dotcloud环境 s ...

  7. 1. 初识ZooKeeper。

    转自:https://blog.csdn.net/en_joker/article/details/78661466 Apache ZooKeeper是由 Apache Hadoop的子项目发展而来, ...

  8. VUE错误记录 - 品牌后台练习 search(); 数组 splice forEach push 箭头函数

    methods:{ add(){ var car = { id: this.id, name: this.name, ctime: new Date()}; this.list.push(car); ...

  9. ThinkPHP5.0的安装

    ThinkPHP5.0的安装很简单: 1.下载“phpstudy”安装 2.下载thinkphp源文件 3.把thinkphp源文件解压并放到phpstudy目录下的“WWW”目录 4.然后开启服务并 ...

  10. 1.1selenium 介绍

    1.1selenium 介绍selenium 是一个 web 的自动化测试工具,不少学习功能自动化的同学开始首选 selenium , 相因为它相比 QTP 有诸多有点:* 免费,也不用再为破解 QT ...