Unity Editor 查找替换 Material材质球

作者:雨辰 发布于:2017-10-17 19:46 Tuesday 分类:Unity3D

using UnityEngine;
using UnityEditor;

public class ShowMaterialUsePrefab : EditorWindow
{
    private static Material _material;

    private static bool isChangeMaterial = false;
    private static Material _changeMaterial;
    //window菜单下
    [MenuItem("Window/查找材质球引用")]
    private static void ShowWindow()
    {
        ShowMaterialUsePrefab cw = GetWindow<ShowMaterialUsePrefab>(true, "查找材质球引用的prefab");
        //(强迫症,看着舒服)
        cw.minSize = new Vector2(310, 200);
        cw.maxSize = new Vector2(310, 300);
    }

    private void OnGUI()
    {
        _material = (Material)EditorGUILayout.ObjectField("材质球", _material, typeof(Material), true);
        if (GUILayout.Button("开始查找", GUILayout.Height(30), GUILayout.Width(300)))
        {
            ShowSelect();
        }

        isChangeMaterial = EditorGUILayout.Toggle("是否替换材质球", isChangeMaterial);
        if (isChangeMaterial)
        {
            _changeMaterial = (Material)EditorGUILayout.ObjectField("要替换的材质球", _material, typeof(Material), true);
            if (GUILayout.Button("开始替换", GUILayout.Height(30), GUILayout.Width(300)))
            {
                ShowSelect();
            }
        }
    }

    public static void ShowSelect() 
    {
        //如果鼠标没有选中物体则返回
        if (Selection.objects == null || Selection.objects.Length == 0) { return; }
        //获取点中对象(包括子目录)所有Material组件
        Object[] skinnedMeshRenderer = Selection.GetFiltered(typeof(Renderer), SelectionMode.Deep);
        Debug.Log("============================== " + _material.name + " ====================================");
        foreach (Object item in skinnedMeshRenderer)
        {
            if (item is UnityEngine.MeshRenderer)
            {
                if (((UnityEngine.MeshRenderer)item).sharedMaterial != null)
                {
                    Material mat = ((UnityEngine.MeshRenderer)item).sharedMaterial;
                    DebugPath(mat, item);
                }
            }
            else if (item is UnityEngine.SkinnedMeshRenderer)
            {
                if (((UnityEngine.SkinnedMeshRenderer)item).sharedMaterial != null)
                {
                    Material mat = ((UnityEngine.SkinnedMeshRenderer)item).sharedMaterial;
                    DebugPath(mat, item);
                }
            }
            else if (item is UnityEngine.ParticleSystemRenderer)
            {
                if (((UnityEngine.ParticleSystemRenderer)item).sharedMaterial != null)
                {
                    Material mat = ((UnityEngine.ParticleSystemRenderer)item).sharedMaterial;
                    DebugPath(mat, item);
                }
            }
        }

        Debug.Log("============================== end ====================================");
    }

    private static void DebugPath(Material mat, Object item) 
    {
        if (mat.name == _material.name && AssetDatabase.GetAssetPath(mat) == AssetDatabase.GetAssetPath(_material))
        {
            if (isChangeMaterial)
            {
                //找到引用
                Debug.Log(" 替换 Selcet Material <" + _material.name + "> : " + AssetDatabase.GetAssetPath(item));
                //查找并替换
                if (item is UnityEngine.MeshRenderer)
                {
                    ((UnityEngine.MeshRenderer)item).sharedMaterial = _changeMaterial;
                }
                else if (item is UnityEngine.SkinnedMeshRenderer)
                {
                    ((UnityEngine.SkinnedMeshRenderer)item).sharedMaterial = _changeMaterial;
                }
                else if (item is UnityEngine.ParticleSystemRenderer)
                {
                    ((UnityEngine.ParticleSystemRenderer)item).sharedMaterial = _changeMaterial;
                }
            }
            else 
            {
                //找到引用
                Debug.Log(" 查找 Selcet Material <" + _material.name + "> : " + AssetDatabase.GetAssetPath(item));
            }
        }
    }
}

标签: Unity3D-Editer

发表评论:

雨辰 joyimp|@2011-2017 京ICP备16030765号