标签:
using UnityEngine; using System.Collections; using UnityEditor; using UnityEditor.Animations; public class CreateAnimator : Editor { [MenuItem("ModelConfig/创建Controller")] static void DoCreateAnimationAssets() { //创建Controller AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath("Assets/GeErZan1.controller"); //得到它的Layer AnimatorControllerLayer layer = animatorController.layers[0]; layer.stateMachine.anyStatePosition = new Vector3(250,-60,0); layer.stateMachine.entryPosition = new Vector3(25, -240, 0); AddParamter(animatorController, "Forward", AnimatorControllerParameterType.Float); AddParamter(animatorController, "StateI", AnimatorControllerParameterType.Int); AddParamter(animatorController, "MoveSpeed", AnimatorControllerParameterType.Float); AddParamter(animatorController, "DeadI", AnimatorControllerParameterType.Int); AddParamter(animatorController, "AttackI", AnimatorControllerParameterType.Int); AddParamter(animatorController, "SkillI", AnimatorControllerParameterType.Int); AddParamter(animatorController, "HurtI", AnimatorControllerParameterType.Int); AddParamter(animatorController, "ControlledI", AnimatorControllerParameterType.Int); //将动画保存到 AnimatorController中 AddState<ComeNormalEnemy>("Come", layer, 1, new Vector3(0, -200, 0)); AnimatorState back = AddState<BackNormalEnemy>("Back", layer, 3, new Vector3(0, -155, 0)); back.speed = -1; AnimatorStateMachine controller = AddStateMachine<ControlledNormalEnemy>("Controlled", layer, new Vector3(0, -110, 0)); AddSubState(controller, layer, "Controlled", 1, 4); AnimatorStateMachine dead = AddStateMachine<DeadNormalEnemy>("Dead", layer, new Vector3(0, -65, 0)); AddSubState(dead,layer, "Dead", 3, 5); AnimatorStateMachine hurt = AddStateMachine<HurtNormalEnemy>("Hurt", layer, new Vector3(0, -20, 0)); AddSubState(hurt, layer, "Hurt", 5, 6); AddState<JumpNormalEnemy>("Jump", layer, 7, new Vector3(0, 25, 0)); AnimatorStateMachine attack = AddStateMachine<AttackNormalEnemy>("Attack", layer, new Vector3(0, 70, 0)); AddSubState(attack, layer, "Attack", 3, 8); AnimatorStateMachine skill = AddStateMachine<SkillNormalEnemy>("Skill", layer, new Vector3(500, -200, 0)); AddSubState(skill, layer, "Skill", 2, 9); AddState<WakeUpNormalEnemy>("WakeUp", layer, 10, new Vector3(500, -155, 0)); AddState<DefenseNormalEnemy>("Defense", layer, 11, new Vector3(500, -110, 0)); AddState<ProvokeNormalEnemy>("Provoke", layer, 12, new Vector3(500, -65, 0)); AddState<AdsorptionNormalEnemy>("Adsorption", layer, 18, new Vector3(500, -20, 0)); AddState<Idle>("Idle", layer, 23, new Vector3(500, 25, 0)); BlendTree blendTree = null; animatorController.CreateBlendTreeInController("Move",out blendTree); } private static AnimatorState AddState<T>(string stateName, AnimatorControllerLayer layer, float threshold, Vector3 position) where T : StateMachineBehaviour { AnimatorStateMachine sm = layer.stateMachine; AnimatorState animatorState = sm.AddState(stateName, position); AnimatorStateTransition animatorStateTransition = sm.AddAnyStateTransition(animatorState); animatorStateTransition.canTransitionToSelf = false; animatorStateTransition.AddCondition(AnimatorConditionMode.Equals, threshold, "StateI"); animatorState.AddStateMachineBehaviour<T>(); return animatorState; } private static AnimatorStateMachine AddStateMachine<T>(string stateName, AnimatorControllerLayer layer, Vector3 position) where T : StateMachineBehaviour { AnimatorStateMachine sm = layer.stateMachine; AnimatorStateMachine sub = sm.AddStateMachine(stateName, position); sub.AddStateMachineBehaviour<T>(); return sub; } private static void AddSubState(AnimatorStateMachine stateMachine, AnimatorControllerLayer layer, string nameStr, int length, float threshold) { Vector3[] positions = new Vector3[length]; int startY = -100; for (int j = 0; j < length; ++j) { positions[j] = new Vector3(300, startY, 0); startY += 70; } for (int i = 0; i < length; ++i) { AnimatorState animatorState = stateMachine.AddState(nameStr + i.ToString(), positions[i]); AnimatorStateMachine sm = layer.stateMachine; AnimatorStateTransition animatorStateTransition = sm.AddAnyStateTransition(animatorState); animatorStateTransition.canTransitionToSelf = false; animatorStateTransition.AddCondition(AnimatorConditionMode.Equals, threshold, "StateI"); animatorStateTransition.AddCondition(AnimatorConditionMode.Equals, i, nameStr + "I"); } } private static void AddParamter(AnimatorController controller, string name, AnimatorControllerParameterType type) { controller.AddParameter(name, type); } }
标签:
原文地址:http://www.cnblogs.com/bzyzhang/p/5721238.html