Unity编辑器实现对选中物体一键替换材质

说明

       最近美术同事在整理模型动画时有一个需求,就是选中部分的模型一件将材质替换成同一个材质球。在编辑器下将这个功能实现了出来。代码放在最后,简单记录一下。

思路

  1. 首先对选中进行遍历,拿到所有的子对象;
  2. 对每个具体的对象拿到对应的Mesh Renderer组件;
  3. 对每个MeshRender组件中的Materials进行判断,判断是否有材质,材质是否有多个的情况;
  4. 在替换成功后支持回退的功能;

效果

       这里是在Transform组件中调出对应的方法,因为有可能选中的物体没有MeshRenderer组件。支持选中多个物体。
在这里插入图片描述
在这里插入图片描述

代码

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


public class HK_ReplaceMaterial : Editor
{
    [MenuItem("CONTEXT/Transform/ReplaceMaterial")]
    static void MaterialReplacing()
    {
        MaterialReplacingEditorPanel.ShowReplaceMaterials();
    }
}

public class MaterialReplacingEditorPanel : EditorWindow
{   
    //要替换的材质
    Material _material;
   
    public static void ShowReplaceMaterials()
    {
        EditorWindow ReplaceMaterialsWindow = GetWindow<MaterialReplacingEditorPanel>("ReplaceMaterials");
        ReplaceMaterialsWindow.Show();
    }

    private void OnGUI()
    {
        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        GUILayout.Label("材质替换", GUILayout.Width(100f));
        _material = EditorGUILayout.ObjectField(_material, typeof(Material), true) as Material;
        GUILayout.EndHorizontal();

        GUILayout.Space(10);

        if (GUILayout.Button("替换"))
        {
            var selectionObjs = Selection.gameObjects;

            if (_material == null)
            {
                EditorUtility.DisplayDialog("提醒", string.Format("当前没有选择任何材质"), "确定");
            }
            else if (selectionObjs.Length == 0)
            {
                EditorUtility.DisplayDialog("提醒", string.Format("当前没有选中物体"), "确定");
        }
            else if (EditorUtility.DisplayDialog("提醒", string.Format("是否将当前选中的物体全部替换材质?", _material.name), "确定", "取消"))
            {
                for (int i = 0; i < selectionObjs.Length; i++)
                {
                    TraverseChild(selectionObjs[i]);
                }
            }
        }
    }

    void TraverseChild(GameObject thisObject)
    {
        //回退
        List<Object> obj = new List<Object>();

        for (int i = 0; i < thisObject.GetComponentsInChildren<Renderer>(true).Length; i++)
        {
            if (thisObject == null)
                continue;

            else if (thisObject.GetComponentsInChildren<Renderer>(true)[i] == null)
                continue;
            else if (thisObject.GetComponentsInChildren<Renderer>(true)[i].sharedMaterial == null)
                continue;

            obj.Add(thisObject.GetComponentsInChildren<Renderer>(true)[i]);
        }

        Undo.RecordObjects(obj.ToArray(), "ds");

        for (int i = 0; i < thisObject.GetComponentsInChildren<Renderer>(true).Length; i++)
        {
            if (thisObject == null)
                continue;
            else if (thisObject.GetComponentsInChildren<Renderer>(true)[i] == null)
                continue;
            
            else if (thisObject.GetComponentsInChildren<Renderer>(true)[i].sharedMaterial == null)
                continue;
            
            List<Material> materials = new List<Material>();

            foreach (var j in thisObject.GetComponentsInChildren<Renderer>(true)[i].sharedMaterials)
            {
                materials.Add(_material);
            }

            thisObject.GetComponentsInChildren<Renderer>(true)[i].sharedMaterials = materials.ToArray();
        }
    }
}

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值