unity3d自動生成動畫狀態(tài)機

因項目需要,人物30多個,每個人物動畫以及命名都是一樣。新版的動畫系統(tǒng)Mecanim,支持連線之類,想在新項目中使用。但是讓策劃一個個手動去連線,設(shè)置觸發(fā)條件太麻煩了也容易出錯。(初步我們有4個動作 "walk", "idle", "attack", "skill",四個動作可以直接相互切換)

網(wǎng)上沒找到類似的代碼。。于是寫了個簡單的插件來處理。直接上代碼。。這個就自動連線和設(shè)置里面的條件了。
先搜索整個目錄下的fbx文件(動作和模型做到一起去了),然后根據(jù)制定的幾個動作,自動生成狀態(tài)機,包括設(shè)置好觸發(fā)條件。

Paste_Image.png

自動生成的生成的動畫狀態(tài)機

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;


public class AutoAnimatorCtl : Editor
{
    [MenuItem("test/AutoAnimatorCtl")]
    static void batchAnimatorCtl()
    {
        string dirPath = "Assets/Media/Character/Model/";
        foreach (string path in Directory.GetFiles(dirPath))
        {
            if (System.IO.Path.GetExtension(path) == ".FBX")
            {
                    createAnimatorCtl(path);
            }
        }
    }
    static bool isUniqueState(string str)
    {
        string[] states = { "walk", "idle", "attack", "skill" };
        foreach (string state in states)
        {
            if (str == state)
                return true;
        }
        return false;
    }
    //檢查fbx里面動畫的完整性
    static bool checkFullAnimation(Object[] objs)
    {
        string[] clipName = { "walk", "idle", "attack", "skill" };
        //Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path);
        int num = 0;
        foreach (Object obj in objs)
        {
            if(obj is AnimationClip && isUniqueState(obj.name))
            {
                num = num + 1;
            }
        }
        if (num == clipName.Length)
            return true;
        return false;
    }
    static void createAnimatorCtl(string path)
    {
        path = path.Replace("\\", "/");
        path = path.Replace(".FBX", ".fbx");
        string acPath = path.Replace(".fbx", ".controller");
        string prePath = path.Replace(".fbx", ".prefab");
        Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path);
        var animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(acPath);
        var layer = animatorController.layers[0];
        var anmationStateMachine = layer.stateMachine;
        List<UnityEditor.Animations.AnimatorState> states = new List<UnityEditor.Animations.AnimatorState>();
        foreach (Object obj in objs)
        {
            if (obj is AnimationClip && isUniqueState(obj.name) == true)
            {
                Debug.Log(obj.name + " is clip");
                var state = anmationStateMachine.AddState(obj.name);
                states.Add(state);
                state.motion = obj as Motion;
            }
        }
        animatorController.AddParameter("walk", AnimatorControllerParameterType.Bool);
        animatorController.AddParameter("skill", AnimatorControllerParameterType.Bool);
        animatorController.AddParameter("attack", AnimatorControllerParameterType.Bool);
        animatorController.AddParameter("idle", AnimatorControllerParameterType.Bool);
        
        for(int i = 0; i< states.Count;i++)
        {
            for(int j = 0; j < states.Count; j++)
            {
                if(i != j)
                {
                    var anstateTras = states[i].AddTransition(states[j], false);
                    anstateTras.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, states[j].name);
                }
            }
            if(states[i].name == "idle")
            {
                anmationStateMachine.defaultState = states[i];
            }
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容