最近想做一个战斗内容的 demo ,来综合一下有关于战斗的学习知识,所以在网上找了到了一些模型
模型是找到了,但是这些模型大多都是没有相关的 Animator Controller 的,所以想着使用 unity 编辑器的 代码来自动生成
模型的 Controller 这样方便又省事。
这是模型的动作

请忽略一下生成的 Controller
然后先建立一个对应的动作枚举,
public enum AnimationID
{
None,
Idle1,
Idle2,
attack1,
attack2,
crit,
dance,
death,
joke,
laugh,
recall,
run,
run2,
spell1,
spell2,
spell3,
spell4,
taunt,
}
以后播放对应动作的时候也需要使用
下面是具体的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.Animations;
using System.Reflection;
public class GenerateAnimatorController
{
private const string ctrlExtention = "controller";
[MenuItem("Assets/GenerateAnimatorController")]
private static void GenerateAnimatorControllerFunc()
{
Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
string[] dirs = new string[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
dirs[i] = AssetDatabase.GetAssetPath(objs[i]);
}
CreateAnimatorController(dirs);
}
private static void CreateAnimatorController(string[] paths)
{
Dictionary<string, int> animationToId = GetAnimationIDDicti();
Dictionary<string, string[]> modelToAnimationFiles = new Dictionary<string, string[]>();
foreach (string dir in paths)
{
if (!Directory.Exists(dir))
{
Debug.LogError("no path : " + dir);
}
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if(dirInfo.GetFiles("*." + ctrlExtention, SearchOption.TopDirectoryOnly).Length > 0)
{
continue;
}
FileInfo[] fileInfos = dirInfo.GetFiles("*@*.fbx", SearchOption.AllDirectories);
if(fileInfos.Length == 0)
{
Debug.LogError("不存在 *@*.fbx 文件");
}
List<string> animationFiles = new List<string>();
foreach (FileInfo item in fileInfos)
{
animationFiles.Add(dir + "/" + item.Name);
}
string controllerName = dir + "/" + dirInfo.Name + "Controller" + "." + ctrlExtention;
modelToAnimationFiles.Add(controllerName, animationFiles.ToArray());
}
foreach (KeyValuePair<string, string[]> keyValue in modelToAnimationFiles)
{
string controllerPath = keyValue.Key;
string[] aniFiles = keyValue.Value;
List<AnimatorState> states = new List<AnimatorState>();
AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
AnimatorStateMachine baseStateMachine = controller.layers[0].stateMachine;
controller.AddParameter("state", AnimatorControllerParameterType.Int);
foreach (string aniFile in aniFiles)
{
Object[] assets = AssetDatabase.LoadAllAssetsAtPath(aniFile);
foreach (Object clip in assets)
{
if (clip.GetType() != typeof(AnimationClip)) continue;
if (!animationToId.ContainsKey(clip.name)) continue;
AnimatorState state = baseStateMachine.AddState(clip.name);
if(animationToId[clip.name] == 0)
{
baseStateMachine.defaultState = state;
}
state.motion = (Motion)clip;
states.Add(state);
}
}
LinkStatesByStatesList(ref states, ref animationToId);
}
}
private static void LinkStatesByStatesList(ref List<AnimatorState> states, ref Dictionary<string, int> animationToId)
{
foreach (AnimatorState exitAnimState in states)
{
foreach (AnimatorState otherState in states)
{
if(otherState.Equals(exitAnimState))
{
if (otherState.name == "die") continue;
AnimatorStateTransition trnas = exitAnimState.AddTransition(otherState);
trnas.duration = otherState.motion.averageDuration;
trnas.canTransitionToSelf = true;
trnas.exitTime = 0;
trnas.AddCondition(AnimatorConditionMode.Equals, animationToId[otherState.name], "state");
continue;
}
else
{
AnimatorStateTransition trans = exitAnimState.AddTransition(otherState);
trans.AddCondition(AnimatorConditionMode.Equals, animationToId[otherState.name], "state");
}
}
}
}
private static Dictionary<string, int> GetAnimationIDDicti()
{
Dictionary<string, int> result = new Dictionary<string, int>();
FieldInfo[] fields = typeof(AnimationID).GetFields(BindingFlags.Static | BindingFlags.Public);
for (int i = 0; i < fields.Length; ++i)
{
result.Add(fields[i].Name, i);
}
return result;
}
}
使用方式是右键选中对应的模型的文件夹,然后就可以生成了
看到这个图,我感觉有点奇怪,因为这不符合我的预期,直到我拖到其中的一些动作的时候,
才发现是符合预期的。
以上就是所有内容了。