在模型中截取多个模型动画,生成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;
}
}
}
}
}