一般动作类游戏的工程目录一定会有这样的目录结构
一般会由策划或者动画师把这么多的动画拉进一个animator中.是不是这位负责人每次都想泪奔到厕所画圈圈.今天就是把这么多步操作简化成程序一键创建。
我用的是unity3d 5.1.1f1
我直接上代码吧~
<pre name="code" class="csharp">[MenuItem("Assets/GameTools/AnimTools/CreateAnimator")]
public static void CreateAnimator()
{
string path;
path = AssetDatabase.GetAssetPath( Selection.activeObject.GetInstanceID ());
path = path.Replace(".FBX","_animator.controller");
Debug.Log ("Path is " + path);
UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath (path);
AnimatorController ac = AssetDatabase.LoadAssetAtPath<AnimatorController> (path);
///Get all animclip from dir
string animclipPath = path.Replace ("/" + ac.name + ".controller", "");
animclipPath = animclipPath.Replace("Assets","");
Debug.Log ("AnimclipPath is " + animclipPath);
string objPath = Application.dataPath + animclipPath;
string[] animclipStr;
Debug.Log ("objPath is " + objPath);
//返回指定的目录中文件和子目录的名称的数组或空数组
animclipStr = System.IO.Directory.GetFileSystemEntries(objPath);
AnimationClip clip;
int animclipCount = 0;
int attackIndex = 0;
int otherIndex = 0;
AnimatorState defaultState = new AnimatorState ();
bool isDazhaoqianExist = false;
string idleClipPath = "";
for (int i = 0; i < animclipStr.Length; i++)
{
if (animclipStr [i].EndsWith (".anim")) {
animclipCount++;
animclipPath = animclipStr [i].Replace ("/client", ";");
string[] pathSplited = animclipPath.Split (';');
path = pathSplited [1];
path = path.Remove (0, 1);
path = path.Replace ('\\', '/');
Debug.Log ("animclipPath is " + path);
clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(path);
Motion m = clip as Motion;
if(clip.name.Contains(""))
if(clip.name.Contains("attack") || clip.name.Contains("skill") || clip.name.Contains("bati"))
{
ac.layers[0].stateMachine.AddState(clip.name,new Vector3(AttackPosXInAnimator,attackIndex * HeightInAnimator,0)).motion = m;
attackIndex++;
}
else if(clip.name == "idle")
{
idleClipPath = AssetDatabase.GetAssetPath(clip.GetInstanceID());
defaultState = ac.layers[0].stateMachine.AddState(clip.name,new Vector3(OtherPosXInAnimator,otherIndex * HeightInAnimator,0));
defaultState.motion = m;
otherIndex++;
}
else
{
ac.layers[0].stateMachine.AddState(clip.name,new Vector3(OtherPosXInAnimator,otherIndex * HeightInAnimator,0)).motion = m;
otherIndex++;
}
}
}
ac.layers [0].stateMachine.defaultState = defaultState;
}
用法是右击原模就是那个唯一的fbx.选择GameTools/AnimTools/CreateAnimator
原理是把被选择的fbx同目录后缀为.anim的文件全部加入到一个新的animatorcontroller里面。然后以idle为最初始的状态。
我这里做了个小处理。把攻击型技能分到了左边,状态类动画分到右边。容易分辨。
PS:如果原目录下有animatorcontroller是会覆盖掉的。所以如果改了animator中的数据例如:skill1的IKFoot是勾上的。那么再次CreateAnimator时数据会丢失。需要重新勾选0 0、