Unity 编辑器开发实战【Model Importer】- 如何多选设置模型导入设置中的Material Location

模型的Import Settings中Materials部分是不支持多选进行编辑的,如图所示,如果我们选中多个模型,编辑器中会提示Material Editing is not supported on multiple selection.

假如我们往工程中导入了大量模型,其默认的Location设置为Use Embedded Materials,而我们想要将其设为Use External Materials(Legacy),就需要依次选中模型、设置Location,比较耗时耗力。

本文实现的工具可以支持多选模型进行Material Location设置,如图所示:

代码如下:

using UnityEngine;
using UnityEditor;

namespace SK.Framework
{
    public class ModelImportSettings : EditorWindow
    {
        private ModelImporterMaterialLocation location;

        [MenuItem("SKFramework/Tools/Model Import Settings")]
        private static void Open()
        {
            GetWindow<ModelImportSettings>("Model Import Settings").Show();
        }

        private void OnGUI()
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("Location");
                location = (ModelImporterMaterialLocation)EditorGUILayout.EnumPopup(location);
            }
            GUILayout.EndHorizontal();

            if (Selection.gameObjects.Length == 0) return;
            GUILayout.FlexibleSpace();

            if (GUILayout.Button("Apply"))
            {
                for (int i = 0; i < Selection.gameObjects.Length; i++)
                {
                    var obj = Selection.gameObjects[i];
                    string path = AssetDatabase.GetAssetPath(obj);
                    ModelImporter importer = AssetImporter.GetAtPath(path) as ModelImporter;
                    if (importer != null)
                    {
                        importer.materialLocation = location;
                    }
                }
            }
        }

        private void OnSelectionChange()
        {
            Repaint();
        }
    }
}

  欢迎关注公众号 “当代野生程序猿”

猜你喜欢

转载自blog.csdn.net/qq_42139931/article/details/123656770