前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity 编辑器开发实战【Model Importer】- 如何多选设置模型导入设置中的Material Location

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

作者头像
CoderZ
发布2022-08-29 16:51:08
9460
发布2022-08-29 16:51:08
举报

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

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

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

代码如下:

代码语言:javascript
复制
using UnityEngine;
using UnityEditor;

namespace SK.Framework
{
    public class ModelImportSettings : EditorWindow
    {
        //Location类型
        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");
                //EnumPopup显示选择的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);
                    //根据路径Asset Importer并转化为Model Importer
                    ModelImporter importer = AssetImporter.GetAtPath(path) as ModelImporter;
                    //判断选中的物体是否为模型
                    if (importer != null)
                    {
                        //将materialLocation设为目标值
                        importer.materialLocation = location;
                    }
                }
            }
        }
        //选中的物体变更时调用Repaint函数重新绘制
        private void OnSelectionChange()
        {
            Repaint();
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档