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

上一篇搞定了自动生成动画,这一篇来试试动画控制器(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创建完整的动画控制器,动画状态,跳转流程,动画片段了。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值