using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor.Sprites;
public class ChangeMaterial : Editor
{
/// <summary>
/// 批量替换预设体材质球 操作步骤 1、 选着材质球 2、按Ctrl 选着预设体文件夹或者预设体 3、右键ChangeMaterial
/// </summary>
[MenuItem("Assets/ChangeMaterial")]
public static void change()
{
Object[] m_objects = Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets);//选择的材质球
Object[] obj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//选择的预设体文件夹或者预设体
foreach (var go in obj)
{
GameObject game = go as GameObject;
if (game != null)
{
FindMater(game, m_objects[0] as Material);
}
}
}
public static void FindMater(GameObject go, Material m)
{
SpriteRenderer[] renderers = go.GetComponentsInChildren<SpriteRenderer>(true);
GameDebugger.instance.GameDebugLog("根物体:", go.name);
for (int i = 0; i < renderers.Length; i++)
{
SpriteRenderer item = renderers[i];
if (item.sharedMaterial != null)
{
if (item.sharedMaterial.name == "Sprites-Default")
{
EditorUtility.DisplayCancelableProgressBar("替换材质:", go.name, i / renderers.Length * 1.0f);//进度条
GameDebugger.instance.GameDebugLog("子物体:", item.name,"材质球: ",item.sharedMaterial.name,"替换材质球:",m.name);
item.sharedMaterial = m;
}
}
EditorUtility.SetDirty(item);
AssetDatabase.SaveAssets();
}
EditorUtility.ClearProgressBar();//关闭进度条
}
}