版本:Unity 6(6000.0)
语言英语
  • C#

动画器控制器.查找状态机行为上下文

提出修改建议

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有申请,但我们会阅读每位用户提出的修改建议,并酌情进行更新。

关闭

提交失败

由于某种原因,您的修改建议无法提交。请在几分钟后重试。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static StateMachineBehaviourContext[] FindStateMachineBehaviourContext(状态机行为 behavior);

参数

behavior 要为其获取上下文的 State Machine Behavior。

返回值

StateMachineBehaviourContext[]返回状态机行为编辑上下文。

说明

使用此函数检索此行为的所有者。

请注意,该函数非常慢。不建议每帧都使用此函数。其他资源:StateMachineBehaviourContext

using UnityEngine;
using UnityEditor;

public class IdleBehaviour : StateMachineBehaviour { public int transitionCount; protected int randomTransitionId = Animator.StringToHash("random");

// OnStateEnter is called when a transition starts and the state machine starts to evaluate the state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { }

// OnStateUpdate is called at each engine tick between OnStateEnter and OnStateExit callback override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (stateInfo.normalizedTime > 0.5f) animator.SetInteger(randomTransitionId, Random.Range(0, transitionCount)); }

// OnStateExit is called when a transition ends and the state machine ends to evaluate the state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { } }

[CustomEditor(typeof(IdleBehaviour), true)] public class IdleBehaviourEditor : Editor { UnityEditor.Animations.StateMachineBehaviourContext[] context;

SerializedProperty transitionCount;

public void OnEnable() { context = UnityEditor.Animations.AnimatorController.FindStateMachineBehaviourContext(target as StateMachineBehaviour); if (context != null) { // animatorObject can be an AnimatorState or AnimatorStateMachine UnityEditor.Animations.AnimatorState state = context[0].animatorObject as UnityEditor.Animations.AnimatorState; if (state != null) { IdleBehaviour behaviour = target as IdleBehaviour; behaviour.transitionCount = state.transitions.Length; } }

transitionCount = serializedObject.FindProperty("transitionCount"); }

public override void OnInspectorGUI() { serializedObject.Update();

EditorGUI.BeginDisabledGroup(true); EditorGUILayout.PropertyField(transitionCount); EditorGUI.EndDisabledGroup();

serializedObject.ApplyModifiedProperties(); } }