仅从FBX模型中提取动画剪辑,然后删除其他所有内容

时间:2018-12-31 21:25:48

标签: c# unity3d

我正在尝试仅从FBX模型中提取动画剪辑。目前,我的脚本能够重命名动画剪辑。将模型导入资源文件夹时,展开模型时可以看到模型和动画剪辑:

Animation clip

在上面的屏幕截图中,当我展开Standing 2H Magic Attack 03时,有动画剪辑本身和一个化身。我只想要动画剪辑,而没有别的。这是我当前的脚本:

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

public class MixamoManager : EditorWindow
{
    private static MixamoManager editor;
    private static int width = 350;
    private static int height = 300;
    private static int x = 0;
    private static int y = 0;
    private static List<string> allFiles = new List<string>();

    [MenuItem("Window/Mixamo Manager")]
    static void ShowEditor()
    {
        editor = EditorWindow.GetWindow<MixamoManager>();
        CenterWindow();
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Rename"))
        {
            Rename();
        }
    }

    public void Rename()
    {
        DirSearch();

        if (allFiles.Count > 0)
        {
            for (int i = 0; i < allFiles.Count; i++)
            {
                int idx = allFiles[i].IndexOf("Assets");
                string filename = Path.GetFileName(allFiles[i]);
                string asset = allFiles[i].Substring(idx);
                AnimationClip orgClip = (AnimationClip)AssetDatabase.LoadAssetAtPath(
                     asset, typeof(AnimationClip));

                var fileName = Path.GetFileNameWithoutExtension(allFiles[i]);
                var importer = (ModelImporter)AssetImporter.GetAtPath(asset);

                RenameAndImport(importer, fileName);
            }
        }
    }

    private void RenameAndImport(ModelImporter asset, string name)
    {
        ModelImporter modelImporter = asset as ModelImporter;
        ModelImporterClipAnimation[] clipAnimations = modelImporter.defaultClipAnimations;

        for (int i = 0; i < clipAnimations.Length; i++)
        {
            clipAnimations[i].name = name;
        }

        modelImporter.clipAnimations = clipAnimations;
        modelImporter.SaveAndReimport();
    }

    private static void CenterWindow()
    {
        editor = EditorWindow.GetWindow<MixamoManager>();
        x = (Screen.currentResolution.width - width) / 2;
        y = (Screen.currentResolution.height - height) / 2;
        editor.position = new Rect(x, y, width, height);
        editor.maxSize = new Vector2(width, height);
        editor.minSize = editor.maxSize;
    }

    static void DirSearch()
    {
        string info = Application.dataPath; //+ "/Mixamo/Animations/medea_m_arrebola/Magic";
        string[] fileInfo = Directory.GetFiles(info, "*.fbx", SearchOption.AllDirectories);
        foreach (string file in fileInfo)
        {
            if (file.EndsWith(".fbx"))
                allFiles.Add(file);
        }
    }
}

任何建议将不胜感激。

0 个答案:

没有答案