返回介绍

StateMachineBehaviour 状态机行为

发布于 2019-12-18 15:38:34 字数 5570 浏览 1198 评论 0 收藏 0

class in UnityEngine/Inherits from: ScriptableObject

Description 描述

tateMachineBehaviour is a component that can be added to a state machine state. It's the base class every script on a state derives from.

状态机行为是个可以添加状态机状态的组件。这是来自基类上的每个脚本派生的状态。

By default the Animator does instantiate a new instance of each behaviour define in the controller. The class attribute SharedBetweenAnimatorsAttribute control how behaviours are instantiated.

默认情况下,Animator示例化一个新的实例的每个行为在控制器中定义。该类属性SharedBetweenAnimatorsAttribute 控制如何实例化该行为。

StateMachineBehaviour has some predefined messages: OnStateEnter, OnStateExit, OnStateIK, OnStateMove, OnStateUpdate.

StateMachineBehaviour 有一些预定义的信息:OnStateEnter, OnStateExit, OnStateIK, OnStateMove, OnStateUpdate。

JavaScript:

#pragma strict
public class AttackBehaviour extends StateMachineBehaviour {
	public var particle: GameObject;
	public var radius: float;
	public var power: float;
	protected var clone: GameObject;
	public override function OnStateEnter(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		clone = Instantiate(particle, animator.rootPosition, Quaternion.identity) as GameObject;
		var rb: var = clone.GetComponent.<Rigidbody>();
		rb.AddExplosionForce(power, animator.rootPosition, radius, 3.0f);
	}
	public override function OnStateExit(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Destroy(clone);
	}
	public override function OnStateUpdate(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Debug.Log("On Attack Update ");
	}
	public override function OnStateMove(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Debug.Log("On Attack Move ");
	}
	public override function OnStateIK(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Debug.Log("On Attack IK ");
	}
}

C#:

using UnityEngine;
 
public class AttackBehaviour : StateMachineBehaviour
{
	public GameObject particle;
	public float radius;
	public float power;
 
	protected GameObject clone;
 
	override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		clone = Instantiate(particle, animator.rootPosition, Quaternion.identity) as GameObject;
		var rb = clone.GetComponent<Rigidbody>();
		rb.AddExplosionForce(power, animator.rootPosition, radius, 3.0f);
	}
	override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Destroy(clone);
	}
	override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Debug.Log("On Attack Update ");
	}
	override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Debug.Log("On Attack Move ");
	}
	override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Debug.Log("On Attack IK ");
	}
}

Public Functions 公共函数

OnStateMachineEnterCalled 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.
变换转进入状态机时调用该属性。变换进入状态机的子状态时不会调用该属性。
OnStateMachineExitCalled 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.
变换超出状态机时调用该属性。变换在状态机的子状态下不会调用该属性。。

Messages 消息

OnStateEnterCalled on the first Update frame when a statemachine evaluate this state.
状态机评估进入状态时调用该属性。
OnStateExitCalled on the last update frame when a statemachine evaluate this state.
当状态机评估停止状态时调用该属性。
OnStateIKCalled right after MonoBehaviour.OnAnimatorIK.
MonoBehaviour.OnAnimatorIK之后调用该属性。
OnStateMoveCalled right after MonoBehaviour.OnAnimatorMove.
MonoBehaviour.OnAnimatorMove之后调用该属性。
OnStateUpdateCalled at each Update frame except for the first and last frame.
除了开始和最后一帧,每次更新时调用。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文