Unity编辑器FBX导出anim文件

前提:
发现网络上没有太新且可用的关于unity编辑器中导出FBX中动画文件的文章参考,所以通过工作积累加api研究,做个小笔记。

工程结构说明:
源文件目录为Assets\Game\Arts\Animations\Source\AAA\BBB.FBX
期望导出目录为Assets\Game\Arts\Animations\Export\AAA\BBB.anim

直接上代码:

using System.IO;
using UnityEditor;
using UnityEngine;

namespace AnimationExporter
{
    public class AnimationExporter
    {
        [MenuItem("Assets/ExportTool/AnimationExport")]
        public static void DoAnimationEexport()
        {
            Object[] arrObj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            foreach(Object o in arrObj)
            {
                string sAssetPath = AssetDatabase.GetAssetPath(o);

                Object[] assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(sAssetPath);
                foreach (Object asset in assets)
                {
                    AnimationClip srcClip = asset as AnimationClip;
                    if (null == srcClip)
                        continue;

                    Debug.Log("Export animation path:" + sAssetPath);
                    string sExportPath = GetAnimationExportPath(sAssetPath);
                    bool isFileExists = File.Exists(sExportPath);

                    AnimationClip exportClip;
                    if (isFileExists)
                    {
                        exportClip = AssetDatabase.LoadAssetAtPath(sExportPath, typeof(AnimationClip)) as AnimationClip;
                    }
                    else
                    {
                        exportClip = new AnimationClip();
                    }

                    EditorUtility.CopySerialized(srcClip, exportClip);

                    if (!isFileExists)
                    {
                        AssetDatabase.CreateAsset(exportClip, sExportPath);
                    }
                    
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }


        /// 
        /// 获得动画导出路径;
        /// 
        /// 导出的资源对象整目录
        /// 导出的动画对象整目录
        private static string GetAnimationExportPath(string path)
        {
            int nFileSplitIndex = path.LastIndexOf("/");
            string sFileName = path.Substring(nFileSplitIndex + 1);

            path = path.Substring(0, nFileSplitIndex);

            //Assets/Game/Arts/Animations....
            string sSplit = "Animations";
            int nIndex = path.LastIndexOf(sSplit);
            nIndex = nIndex + sSplit.Length;
            string sPrefixPath = path.Substring(0, nIndex);
            //(Assets/Game/Arts/Animations/Source) + sSplitPath
            string sSourceName = "Source";
            int nSplitSourceName = path.LastIndexOf(sSourceName);
            nSplitSourceName = nSplitSourceName + sSourceName.Length;
            string sSplitPath = path.Substring(nSplitSourceName + 1);
            //Assets/Game/Arts/Animations/Export/ + sSplitPath
            string sExportPath = sPrefixPath + "/Export/" + sSplitPath;

            Directory.CreateDirectory(sExportPath);

            sExportPath = sExportPath + "/" + sFileName;
            sExportPath = sExportPath.Replace(".FBX", ".anim");
            return sExportPath;
        }
    }
}

你可能感兴趣的:(Unity编辑器FBX导出anim文件)