最近想做一個戰鬥内容的 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;
}
}
使用方式是右鍵選中對應的模型的檔案夾,然後就可以生成了
看到這個圖,我感覺有點奇怪,因為這不符合我的預期,直到我拖到其中的一些動作的時候,
才發現是符合預期的。
以上就是所有内容了。