Unity中FBX动作提取

有一个固定的AnimatorController,会在需要生成的的地方生成AnimatorOverrideController,并把动作按现有文件替换。

第二部分为生成prefab,目前只做了一部分,等待编辑!


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

/*
 * 模型动作提取
 * 需要所提取的动作所在的FBX文件名字一样,动作名一样
 * 7.31   lxf
 */

public class EditorAnimator : EditorWindow
{
    //Base AnimatorController
    public Object m_BaseAnimator;
    ///  需要设置的路径 
    public string path = Application.dataPath + "/DCGame/Res/RoleModel/";

    ///  设置的动画控件的名字 
    private string folderName;
    ///  设置的动画控件的路径 
    private string folderPath;

    ///  所要设置的文件夹 
    DirectoryInfo directory;
    ///  所要设置的文件夹下面的文件夹 
    DirectoryInfo[] diArr;

    ///  遍历出的clips 
    private Dictionary clips;

    private AnimatorController mAnimator;

    [MenuItem("Window/动画编辑")]
    static void Initialize()
    {
        EditorAnimator window = (EditorAnimator)EditorWindow.GetWindow(typeof(EditorAnimator), false, "动画编辑");
        window.Show(true);
    }

    void OnGUI()
    {
        EditorGUILayout.Space();
        GUILayout.Label("模板-动画");
        m_BaseAnimator = EditorGUILayout.ObjectField(m_BaseAnimator, typeof(Object), true) ;
        mAnimator = (AnimatorController)m_BaseAnimator;
        EditorGUILayout.Space();

        GUILayout.Label("文件路径");
        EditorGUILayout.TextField(path);

        EditorGUILayout.Space();

        GUILayout.Label("重新生成可覆盖");
        EditorGUILayout.Space();

        if (GUILayout.Button("开始"))
        {
            SetButton();
        }
    }

    void SetButton()
    {
        if (m_BaseAnimator == null)
        {
            Debug.LogError("模板不可为空");
            return;
        }

        directory = new DirectoryInfo(path);
        diArr = directory.GetDirectories();
        for (int i = 0; i < 2; i++)     //只生成两个测试   diArr.Length
        {
            folderName = diArr[i].Name;
            folderPath = "Assets/DCGame/Res/RoleModel/" + folderName + "/";
            AnimatorOverrideController animatorOverrideController = new AnimatorOverrideController();
            //设置AnimatorController
            animatorOverrideController.runtimeAnimatorController = mAnimator;
            //获得AnimatorController下的clips
            AnimationClipPair[] animationClip = animatorOverrideController.clips;
            clips = AnimLoad(folderPath, "Shenyuan_ani.FBX");
            foreach (var actionName in clips.Keys)
            {
                if (clips.ContainsKey(actionName))
                {
                    AnimationClip outClip;
                    clips.TryGetValue(actionName, out outClip);
                    animatorOverrideController[actionName] = outClip;
                }
            }
            AssetDatabase.CreateAsset(animatorOverrideController, folderPath + folderName + ".overrideController");
            //break;
        }
    }


    /// 
    /// 获取fbx中的动画文件
    /// 
    /// 
    /// 
    Dictionary AnimLoad(string fbxPath,string fbxName)
    {
        Dictionary clips = new Dictionary();
        Object[] objs = AssetDatabase.LoadAllAssetsAtPath(fbxPath + fbxName);

        foreach (Object o in objs)
        {
            AnimationClip clip = o as AnimationClip;
            if (clip != null)
            {
               clips.Add(clip.name, clip);
            }
        }
        return clips;
    }

    /// 
    /// 清理所生成的animatorOverrideController
    /// 
    void CleanAnimatorOverrideController()
    {
        Debug.Log("重新生成会覆盖之前");
    }


    /// 
    /// 创建prefab
    /// 
    /// 
    /// 
    private void CreatePrefab(string path,string name)
    {
        //获取过滤后的
        Object[] obj = Selection.GetFiltered(typeof(GameObject), SelectionMode.Deep);
        Debug.Log(obj.Length);
        for (int i = 0; i < obj.Length; i++)
        {
            GameObject tempObj = obj[i] as GameObject;
            //创建prefab到指定文件夹中
            PrefabUtility.CreatePrefab(path + name + ".prefab", tempObj);
            //刷新
            AssetDatabase.Refresh();   
        }
    }
	
}


你可能感兴趣的:(Unity)