一聚教程网:一个值得你收藏的教程网站

热门教程

Unity如何批量修改FBX模型 Unity批量修改FBX模型代码方法

时间:2022-06-25 02:54:01 编辑:袖梨 来源:一聚教程网

Unity如何批量修改FBX模型?本篇文章小编给大家分享一下Unity批量修改FBX模型代码方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

由于模型数量有点多,并且都要修改参数,还有从里面提取动画。就搜搜查查,搞了个小工具,批量的修改 FBX 模型的 参数,以及提取动画相关。

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class ModifyMoidel : Editor
{
    [MenuItem("BenBen/修改模型ModelScal")]
    public static void ModifyMoidelScale()
    {
        List paths = new List();
        foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.Assets))
        {
            Debug.Log(o.name);
            //非对象不继续
            if (!(o is GameObject))
                continue;
            //将o作为模型存储在mod中
            //Debug.LogWarning(o.name);
            GameObject mod = o as GameObject;
            //将mod模型路径存储在path中
            string path = AssetDatabase.GetAssetPath(mod);
            ModelImporter modelimporter = ModelImporter.GetAtPath(path) as ModelImporter;
            if (!modelimporter)
            {
                UnityEngine.Debug.LogError(string.Format("path-->{0}<---不是ModelImporter", path));
                continue;
            }
            //修改Model 下的Scale Factor
            modelimporter.globalScale = 10;
            paths.Add(path);
            AssetDatabase.ImportAsset(path);
        }
        AssetDatabase.Refresh();
        CreatNewAnimations(paths);
    }
    private static void CreatNewAnimations(List paths)
    {
        UnityEditor.Animations.AnimatorController animatorController = null;
        UnityEditor.Animations.AnimatorControllerLayer layer = null;
        UnityEditor.Animations.AnimatorStateMachine asm = null;
        Debug.Log(paths.Count);
        for (int i = 0; i < paths.Count; i++)
        {
            paths[i].Replace("", "/");
            AnimationClip newClip = new AnimationClip();
            AnimationClip clip = AssetDatabase.LoadAssetAtPath(paths[i], typeof(AnimationClip)) as AnimationClip;
            if (!clip)
            {
                UnityEngine.Debug.LogError(string.Format("path-->{0}<--不包含AnimationClip", paths[i]));
                continue;
            }
            string fbxName = Path.GetFileNameWithoutExtension(paths[i]);
            fbxName = fbxName.Substring(fbxName.LastIndexOf("_") + 1);
            //新的AnimationClip名字 
            var newClipName = GetAniName(int.Parse(fbxName)) + ".anim";
            string directoryPath = paths[i].Replace(Path.GetFileName(paths[i]), "AnimationClip/");
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            EditorCurveBinding[] binding = AnimationUtility.GetCurveBindings(clip);
            for (int j = 0; j < binding.Length; j++)
            {
                AnimationCurve animationCurve = AnimationUtility.GetEditorCurve(clip, binding[j]);
                AnimationUtility.SetEditorCurve(newClip, binding[j], animationCurve);
            }
            //非Legacy动画使用GetCurveBindings、GetEditorCurve和SetEditorCurve方法
			//Legacy要使用GetObjectReferenceCurveBindings、GetObjectReferenceCurve和SetObjectReferenceCurve方法
            //设置AnimationClipSettings
            AnimationUtility.SetAnimationClipSettings(newClip, AnimationUtility.GetAnimationClipSettings(clip));
            string newClipPath = directoryPath + newClipName;
            AssetDatabase.CreateAsset(newClip, newClipPath);
            //生成animator
            if (!animatorController)
            {
                animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(directoryPath + "Animator.controller");
                layer = animatorController.layers[0];
                asm = layer.stateMachine;
            }
            //添加到Animator中
            UnityEditor.Animations.AnimatorState state = asm.AddState(newClip.name);
            state.motion = newClip;
            //如果是Idle 动画,设置loop
            if (newClip.name == "Idle")
            {
                AnimationClipSettings animationClipSettings = AnimationUtility.GetAnimationClipSettings(newClip);
                animationClipSettings.loopTime = true;
                AnimationUtility.SetAnimationClipSettings(newClip, animationClipSettings);
                layer.stateMachine.defaultState = state;
            }
            AssetDatabase.ImportAsset(paths[i]);
        }
        AssetDatabase.Refresh();
    }
    private static string GetAniName(int count)
    {
        switch (count)
        {
            case 1:
                return "Idle";
            case 2:
                return "2-1";
            case 3:
                return "2-2";
            case 4:
                return "2-3";
            case 5:
                return "2-4";
            default:
                return "";
        }
    }
}

热门栏目