AnimatorCondition
структура в UnityEditor.Animations
Описание
Условие, определяющее, будет ли выполнен переход.
Условия Animator представляют собой сравнение между параметром аниматора и пороговым значением. Когда условие назначается переходу, аниматор оценивает условие, чтобы определить, будет ли принят переход. Когда условие выполняется, переход принимается.
Условие состоит из трех частей: режим сравнения, имя параметра и порог. Параметр сравнивается с порогом с помощью сравнения. Параметр всегда находится слева от сравнения. Например, условие с сравнением Greater оценивается как true, если параметр больше порога.
Не все типы параметров совместимы со всеми режимами сравнения. Если вы попытаетесь использовать тип параметра с несовместимым режимом сравнения, возникнет ошибка. Типы параметров и их совместимые режимы сравнения следующие:
- Для параметров с плавающей запятой режимы
GreaterиLessсовместимы. - Для параметров int совместимы режимы
Greater,Less,EqualsиNotEquals. - Для булевых параметров режимы
IfиIfNotсовместимы. - Для параметров триггера совместим режим
If.
Обратите внимание, что когда режим сравнения If или IfNot, пороговое значение игнорируется.
В следующем примере добавляется пункт меню, который создаёт машину состояний в редакторе. В нём используются условия аниматора для управления переходом.
using UnityEditor; using UnityEditor.Animations; using UnityEngine; public static class AnimatorConditionExample { [MenuItem("Example/CreateFancyController")] static void CreateController() { AnimatorController controller = new AnimatorController(); controller.AddLayer("Locomotion"); AnimatorState stateWalk = controller.layers[0].stateMachine.AddState("Walk"); AnimatorState stateJump = controller.layers[0].stateMachine.AddState("Jump"); AnimatorState stateDead = controller.layers[0].stateMachine.AddState("Dead"); controller.AddParameter("StartJump", AnimatorControllerParameterType.Trigger); controller.AddParameter("Health", AnimatorControllerParameterType.Int); controller.layers[0].stateMachine.AddEntryTransition(stateWalk); // Use the conditions property to get the current conditions or set new ones. // Here, the state machine transitions from walk to jump if the StartJump trigger is set. // Because this is an If condition mode, you don't have to set a threshold value. AnimatorStateTransition transitionWalkToJump = stateWalk.AddTransition(stateJump); transitionWalkToJump.conditions = new[] { new AnimatorCondition { mode = AnimatorConditionMode.If, parameter = "StartJump", } }; // Transition to dead if the healh parameter is below 1 AnimatorStateTransition transitionWalkToDead = stateWalk.AddTransition(stateDead); transitionWalkToDead.conditions = new[] { new AnimatorCondition { mode = AnimatorConditionMode.Less, parameter = "Health", threshold = 1, } }; // Consider using AddCondition as a shorthand to add a new AnimatorCondition to the conditions list AnimatorStateTransition transitionJumpToDead = stateJump.AddTransition(stateDead); transitionJumpToDead.AddCondition(AnimatorConditionMode.Less, 1, "Health"); // If no conditions are specified, the transition must have an exit time to be valid AnimatorStateTransition transitionJumpToWalk = stateJump.AddTransition(stateWalk); transitionJumpToWalk.hasExitTime = true; AssetDatabase.CreateAsset(controller, AssetDatabase.GenerateUniqueAssetPath("Assets/FancyController.controller")); } }
Дополнительные ресурсы: ,AnimatorStateMachine,, ,AnimatorTransition,.