5.3.4_FBX自动生成AnimatorController

在模型中截取多个模型动画,生成AnimatorController动画控制器:

using System.Collections;

using UnityEditor;

using UnityEngine;

using System.Collections.Generic;

public class BuildAnimator : Editor {

    static List<string> selectsName = new List<string>();

    static UnityEditor.Animations.AnimatorController animatorController;

    //同一个模型,多个文件,导出一个AnimatorController

    [MenuItem("Build/BuildAnimator_Models")]

    static void DoCreateAnimationModels() {

        //储存选择的文件

        Object[] selects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        //创建animationController文件,名称为animator,保存在Assets路径下

        animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/animator.controller");

        //得到它的Layer, 默认layer为base,layers[0]

        UnityEditor.Animations.AnimatorControllerLayer layer = animatorController.layers[0];

        for (int i = 0; i < selects.Length; i++)  {

            //储存所有的文件路径

            selectsName.Add(AssetDatabase.GetAssetPath(selects[i]));

            Debug.Log(selectsName[i]);

        }

        //创建AnimatorController

        AddStatesTransition(selectsName, layer);

    }

    //不同的动画在一个模型文件中

    [MenuItem("Build/BuildAnimator")]

    static void SelectModelBuildAnimatorController()    {

        Object[] selects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        for (int i = 0; i < selects.Length; i++)        {

            //储存选定的文件的路径

            string objPath = AssetDatabase.GetAssetPath(selects[i]);

            Debug.Log(objPath);

            string buildPath = objPath + "_animator.controller";

            //创建animationController文件,名称为animator,保存在文件路径下

            animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(buildPath);

            //得到它的Layer, 默认layer为base,layers[0]

            UnityEditor.Animations.AnimatorControllerLayer layer = animatorController.layers[0];

            //创建AnimatorController

            AddStateTransition(objPath, layer);

        }

    }

    private static void AddStateTransition(string path, UnityEditor.Animations.AnimatorControllerLayer layer)    {

        //创建动画状态机

        UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;

        //将文件所有信息存入motion中

        Object[] motion = AssetDatabase.LoadAllAssetsAtPath(path) as Object[];

        foreach (Object mo in motion)        {

            if (mo is Motion && !mo.name.Contains("Take 001"))     {

                Debug.Log(mo.name);

                //取出动画名子 添加到状态机里面

                UnityEditor.Animations.AnimatorState state = sm.AddState(mo.name);

                //增加Parameters参数为bool类型

                animatorController.AddParameter(mo.name,  AnimatorControllerParameterType.Bool);

                //添加动画文件到状态机的Motion中

                state.motion = mo as Motion;

                //增加MakeTransition到状态机上

                state.mirrorParameter = mo.name;

                //从AnyState

                //UnityEditor.Animations.AnimatorStateTransition trans = sm.AddAnyStateTransition(state);

                //从Entry

                //UnityEditor.Animations.AnimatorTransition trans = sm.AddEntryTransition(state);

                //删除时间条件

                //trans.hasExitTime = false;

            }

        }

    }

    private static void AddStatesTransition(List<string> path, UnityEditor.Animations.AnimatorControllerLayer layer)    {

        //创建动画状态机

        UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;

        for (int i = 0; i < path.Count; i++)    {

            //将文件所有信息存入motion中

            Object[] motion = AssetDatabase.LoadAllAssetsAtPath(path[i]) as Object[];

            foreach (Object mo in motion)       {

                if (mo is Motion && !mo.name.Contains("Take 001"))      {

                    Debug.Log(mo.name);

                    //取出动画名子 添加到状态机里面

                    UnityEditor.Animations.AnimatorState state = sm.AddState(mo.name);

                    //增加Parameters参数为bool类型

                    animatorController.AddParameter(mo.name, AnimatorControllerParameterType.Bool);

                    //添加动画文件到状态机的Motion中

                    state.motion = mo as Motion;

                    //增加MakeTransition到状态机上

                    //从AnyState

                    //UnityEditor.Animations.AnimatorStateTransition trans = sm.AddAnyStateTransition(state);

                    //从Entry

                    //UnityEditor.Animations.AnimatorTransition trans = sm.AddEntryTransition(state);

                    //删除时间条件

                    //trans.hasExitTime = false;

                }

            }

        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值