亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity 如何批量修改FBX模型

 更新時(shí)間:2021年04月10日 13:12:12   投稿:jingxian  
這篇文章主要介紹了Unity 批量修改FBX模型的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

由于模型數(shù)量有點(diǎn)多,并且都要修改參數(shù),還有從里面提取動(dòng)畫(huà)。就搜搜查查,搞了個(gè)小工具,批量的修改 FBX 模型的 參數(shù),以及提取動(dòng)畫(huà)相關(guān)。

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<string> paths = new List<string>();
        foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.Assets))
        {
            Debug.Log(o.name);
            //非對(duì)象不繼續(xù)
            if (!(o is GameObject))
                continue;
            //將o作為模型存儲(chǔ)在mod中
            //Debug.LogWarning(o.name);
            GameObject mod = o as GameObject;
            //將mod模型路徑存儲(chǔ)在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<string> 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動(dòng)畫(huà)使用GetCurveBindings、GetEditorCurve和SetEditorCurve方法
			//Legacy要使用GetObjectReferenceCurveBindings、GetObjectReferenceCurve和SetObjectReferenceCurve方法
            //設(shè)置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 動(dòng)畫(huà),設(shè)置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 "";
        }
    }
}

如果有更簡(jiǎn)單的實(shí)現(xiàn)方法歡迎各位大佬留言。

補(bǔ)充:Unity 動(dòng)態(tài)修改prefab 同步fbx

有時(shí)候我們想prefab和fbx無(wú)縫切換怎么辦,也就是在unity里調(diào)完效果后不滿意,返回dcc如阿健修改模型,但是prefab上又掛載了東西,不想重拖怎么辦?這時(shí)候prefab variant就又用途了

建立變體,把變體拖到場(chǎng)景里。

或者先拖fbx到場(chǎng)景,再選擇變體放置。

然后修改好模型后,直接再在文件瀏覽器里替換同名prefab

貌似需要到prefab模式里再revert一下

這樣就可以實(shí)現(xiàn)“動(dòng)態(tài)”修改perfab了。沒(méi)改變的話可以右鍵相關(guān)屬性revert

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論