动画控制器 (Animator Controller) 通过具有状态机(由参数控制)的层来控制动画。
using UnityEngine; using UnityEditor; using UnityEditor.Animations; using System.Collections;
// Create a menu item that causes a new controller and statemachine to be created.
public class SM : MonoBehaviour { [MenuItem("MyMenu/Create Controller")] static void CreateController() { // Creates the controller var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/Mecanim/StateMachineTransitions.controller");
// Add parameters controller.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger); controller.AddParameter("Reset", AnimatorControllerParameterType.Trigger); controller.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger); controller.AddParameter("GotoC", AnimatorControllerParameterType.Trigger);
// Add StateMachines var rootStateMachine = controller.layers[0].stateMachine; var stateMachineA = rootStateMachine.AddStateMachine("smA"); var stateMachineB = rootStateMachine.AddStateMachine("smB"); var stateMachineC = stateMachineB.AddStateMachine("smC");
// Add States var stateA1 = stateMachineA.AddState("stateA1"); var stateB1 = stateMachineB.AddState("stateB1"); var stateB2 = stateMachineB.AddState("stateB2"); stateMachineC.AddState("stateC1"); var stateC2 = stateMachineC.AddState("stateC2"); // don’t add an entry transition, should entry to state by default
// Add Transitions var exitTransition = stateA1.AddExitTransition(); exitTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow"); exitTransition.duration = 0;
var resetTransition = rootStateMachine.AddAnyStateTransition(stateA1); resetTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "Reset"); resetTransition.duration = 0;
var transitionB1 = stateMachineB.AddEntryTransition(stateB1); transitionB1.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoB1"); stateMachineB.AddEntryTransition(stateB2); stateMachineC.defaultState = stateC2; var exitTransitionC2 = stateC2.AddExitTransition(); exitTransitionC2.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow"); exitTransitionC2.duration = 0;
var stateMachineTransition = rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineC); stateMachineTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoC"); rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineB); } }
layers | 控制器中的层。 |
parameters | 用于在脚本和控制器之间通信的参数。例如,它们用于驱动过渡和混合树。 |
AnimatorController | 构造函数。 |
AddEffectiveStateMachineBehaviour | Adds a state machine behaviour class of a specific type to the AnimatorState for layer layerIndex. This method should be used when you are dealing with synchronized layer and would like to add a state machine behaviour on a synchronized layer. Note that there is no corresponding "Remove" method. To remove a state machine behaviour, use Object.Destroy. |
AddLayer | Utility 函数,用于向控制器添加一个层。 |
AddMotion | Utility 函数,用其中的运动创建一个新状态。 |
AddParameter | Utility 函数,用于向控制器添加一个参数。 |
CreateBlendTreeInController | 在新 AnimatorState 中创建一个 BlendTree。 |
GetBehaviours | 返回所有与类型 T 匹配或派生自 T 的 StateMachineBehaviour。 |
GetStateEffectiveBehaviours | 获取 AnimatorState 的有效状态机行为列表。行为或者存储在 AnimatorStateMachine 中,或者存储在 AnimatorLayer 的重载中。使用此函数可获取有效使用的 Behaviour 列表。 |
GetStateEffectiveMotion | 获取 AnimatorState 的有效 Motion。Motion 或者存储在 AnimatorStateMachine 中,或者存储在 AnimatorLayer 的重载中。使用此函数可获取有效使用的 Motion。 |
MakeUniqueLayerName | 为层创建唯一名称。 |
MakeUniqueParameterName | 为参数创建唯一名称。 |
RemoveLayer | Utility 函数,用于从控制器删除一个层。 |
RemoveParameter | Utility 函数,用于从控制器删除一个参数。 |
SetStateEffectiveBehaviours | 为 AnimatorState 设置有效的状态机 Behaviour 列表。Behaviour 列表或者存储在 AnimatorStateMachine 中,或者存储在 AnimatorLayer 的重载中。使用此函数可设置有效使用的 Behaviour 列表。 |
SetStateEffectiveMotion | 设置 AnimatorState 的有效 Motion。Motion 或者存储在 AnimatorStateMachine 中,或者存储在 AnimatorLayer 的重载中。使用此函数可设置有效使用的 Motion。 |
CreateAnimatorControllerAtPath | 在给定路径处创建一个 AnimatorController。 |
CreateAnimatorControllerAtPathWithClip | 在给定路径处创建一个 AnimatorController,并自动创建一个带有 AnimatorStateMachine 的 AnimatorLayer,该状态机使用其中的 AnimationClip 添加一个 State。 |
CreateStateMachineBehaviour | 此函数将根据此脚本中的类定义创建一个 StateMachineBehaviour 实例。 |
FindStateMachineBehaviourContext | 使用此函数可检索行为的所有者。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
animationClips | 检索控制器使用的所有 AnimationClip。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.