關於unity3d動畫控制器的一些小工具的寫法
一般動作類遊戲的工程目錄一定會有這樣的目錄結構
一般會由策劃或者動畫師把這麼多的動畫拉進一個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、