Unity3D自动生成动画控制(AnimationController)

发表于2018-09-27
评论0 6.6k浏览
上一篇给大家介绍了自动生成动画,这一篇来介绍下如何自动生成动画控制器(AnimationController)。

代码大部分来自于官方代码:
using System.Collections;
using UnityEditor.Animations;
using UnityEngine;
using UnityEditor;
public class CreateAnimatorController : Editor {
    [MenuItem("Animator/Create Animator Controller")]
    static void CreateController()
    {
        // Creates the controller
        AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath("Assets/Animation/Motion.controller");
        // Add Animation Clips
        AnimationClip clip = new AnimationClip ();
        clip.frameRate = 24;
        AssetDatabase.CreateAsset (clip, "Assets/Animation/Motion.anim");
        AssetDatabase.SaveAssets ();
        // Add parameters
        controller.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("Reset", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("GotoC", AnimatorControllerParameterType.Trigger);
        // Add StateMachines
        AnimatorStateMachine rootStateMachine = controller.layers[0].stateMachine;
        AnimatorStateMachine stateMachineA = rootStateMachine.AddStateMachine("smA");
        AnimatorStateMachine stateMachineB = rootStateMachine.AddStateMachine("smB");
        AnimatorStateMachine stateMachineC = stateMachineB.AddStateMachine("smC");
        // Add Clip
        // Add States
        AnimatorState stateA1 = stateMachineA.AddState("stateA1");
        AnimatorState stateB1 = stateMachineB.AddState("stateB1");
        AnimatorState stateB2 = stateMachineB.AddState("stateB2");
        stateMachineC.AddState("stateC1");
        AnimatorState stateC2 = stateMachineC.AddState("stateC2"); // don’t add an entry transition, should entry to state by default
        // Add clip
        stateA1.motion = clip;
        // Add Transitions
        AnimatorStateTransition exitTransition = stateA1.AddExitTransition();
        exitTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow");
        exitTransition.duration = 0;
        AnimatorStateTransition resetTransition = rootStateMachine.AddAnyStateTransition(stateA1);
        resetTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "Reset");
        resetTransition.duration = 0;
        AnimatorTransition transitionB1 = stateMachineB.AddEntryTransition(stateB1);
        transitionB1.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoB1");
        stateMachineB.AddEntryTransition(stateB2);
        stateMachineC.defaultState = stateC2;
        AnimatorStateTransition exitTransitionC2 = stateC2.AddExitTransition();
        exitTransitionC2.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow");
        exitTransitionC2.duration = 0;
        AnimatorTransition stateMachineTransition = rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineC);
        stateMachineTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoC");
        rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineB);
    }


需要添加AnimationClip可以参照代码里面的方法,我就不添加可以使用的示例动画片段了,结合上一篇的内容,我们可以动态的为Prefab创建完整的动画控制器,动画状态,跳转流程,动画片段了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引