一个很简单的工具,因为在批量替换shader时发现有些时候会丢失贴图和参数(shadergraph好像是这样),所以做了贴图和参数对位继承。
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class ReplaceShaderByFileDir : EditorWindow
{
Shader shader;
Shader originShader;
public Texture texture1, texture2, texture3, texture4, texture5;
string oriTexName1, oriTexName2, oriTexName3, oriTexName4, oriTexName5;
string curTexName1, curTexName2, curTexName3, curTexName4, curTexName5;
bool isShowReplaceGo = false; //是否显示被替换的物体
string tipMsg = null;
MessageType tipMsgType = MessageType.Info;
List<GameObject> replaceGoList = new List<GameObject>();
int matCount = 0; //材质的数量
Vector2 scrollPos = Vector2.zero;
[MenuItem("Editor/替换场景中的shader")]
public static void OpenWindow()
{
ReplaceShaderByFileDir window = (ReplaceShaderByFileDir)EditorWindow.GetWindow<ReplaceShaderByFileDir>(false, "替换场景中的shader");
window.Show();
}
[SerializeField]
protected List<string> _oriTex = new List<string>();
[SerializeField]
protected List<string> _curTex = new List<string>();
//序列化对象
protected SerializedObject _serializedObject;
protected SerializedObject _serializedObject1;
//序列化属性
protected SerializedProperty _assetLstProperty;
protected SerializedProperty _assetLstProperty1;
protected void OnEnable()
{
//使用当前类初始化
_serializedObject = new SerializedObject(this);
_serializedObject1 = new SerializedObject(this);
//获取当前类中可序列话的属性
_assetLstProperty = _serializedObject.FindProperty("_oriTex");
_assetLstProperty1 = _serializedObject1.FindProperty("_curTex");
}
void OnGUI()
{
//更新
_serializedObject.Update();
_serializedObject1.Update();
//开始检查是否有修改
EditorGUI.BeginChangeCheck();
//显示属性
//第二个参数必须为true,否则无法显示子节点即List内容
EditorGUILayout.PropertyField(_assetLstProperty, true);
EditorGUILayout.PropertyField(_assetLstProperty1, true);
//结束检查是否有修改
if (EditorGUI.EndChangeCheck())
{
//提交修改
_serializedObject.ApplyModifiedProperties();
_serializedObject1.ApplyModifiedProperties();
}
GUILayout.Label("原shader:");
originShader = (Shader)EditorGUILayout.ObjectField(originShader, typeof(Shader), true);
GUILayout.Label("替换shader :");
shader = (Shader)EditorGUILayout.ObjectField(shader, typeof(Shader), true);
GUILayout.Space(8);
GUILayout.BeginHorizontal();
if (GUILayout.Button("批量替换", GUILayout.Height(30)))
{
Replace();
}
if (GUILayout.Button("重置", GUILayout.Height(30)))
{
Reset();
}
GUILayout.EndHorizontal();
//提示信息
if (!string.IsNullOrEmpty(tipMsg))
{
EditorGUILayout.HelpBox(tipMsg, tipMsgType);
}
isShowReplaceGo = GUILayout.Toggle(isShowReplaceGo, "显示被替换的GameObject");
if (isShowReplaceGo)
{
if (replaceGoList.Count > 0)
{
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height - 200));
foreach (var go in replaceGoList)
{
EditorGUILayout.ObjectField(go, typeof(GameObject), true);
}
GUILayout.EndScrollView();
}
else
{
EditorGUILayout.LabelField("替换个数为0");
}
}
}
void Replace()
{
replaceGoList.Clear();
if (shader == null)
{
tipMsg = "shader为空!";
tipMsgType = MessageType.Error;
return;
}
if (originShader == null)
{
tipMsg = "指定的shader为空!";
tipMsgType = MessageType.Error;
return;
}
else if (originShader.Equals(shader))
{
tipMsg = "替换的shader和指定的shader相同!";
tipMsgType = MessageType.Error;
return;
}
Dictionary<GameObject, Material[]> matDict = GetAllScenceMaterial();
List<Material> replaceMatList = new List<Material>();
foreach (var item in matDict)
{
GameObject tempGo = item.Key;
Material[] mats = item.Value;
int length = mats.Length;
for (int i = 0; i < length; i++)
{
var mat = mats[i];
if (mat != null && mat.shader.Equals(originShader))
{
if (!mat.shader.Equals(shader))
{
replaceGoList.Add(tempGo);
if (!replaceMatList.Contains(mat))
replaceMatList.Add(mat);
}
}
}
}
int replaceMatCount = replaceMatList.Count; //替换Material的数量
for (int i = 0; i < replaceMatCount; i++)
{
UpdateProgress(i, replaceMatCount, "替换中...");
//Get方法获取原贴图
if (_oriTex.Count > 0)
{
oriTexName1 = _oriTex[0];
texture1 = replaceMatList[i].GetTexture(oriTexName1);
if (_oriTex.Count > 1)
{
oriTexName2 = _oriTex[1];
texture2 = replaceMatList[i].GetTexture(oriTexName2);
if (_oriTex.Count > 2)
{
oriTexName3 = _oriTex[2];
texture3 = replaceMatList[i].GetTexture(oriTexName3);
if (_oriTex.Count > 3)
{
oriTexName4 = _oriTex[3];
texture4 = replaceMatList[i].GetTexture(oriTexName4);
if (_oriTex.Count > 4)
{
oriTexName5 = _oriTex[4];
texture5 = replaceMatList[i].GetTexture(oriTexName5);
}
}
}
}
}
//替换shader
replaceMatList[i].shader = shader;
//Set方法设置新贴图
if (_curTex.Count > 0)
{
curTexName1 = _curTex[0];
replaceMatList[i].SetTexture(curTexName1, texture1);
if(_curTex.Count > 1)
{
curTexName2 = _curTex[1];
replaceMatList[i].SetTexture(curTexName2, texture2);
if(_curTex .Count > 2)
{
curTexName3 = _curTex[2];
replaceMatList[i].SetTexture(curTexName3, texture3);
if(_curTex .Count > 3)
{
curTexName4 = _curTex[3];
replaceMatList[i].SetTexture(curTexName4, texture4);
if(_curTex .Count > 4)
{
curTexName5 = _curTex[4];
replaceMatList[i].SetTexture(curTexName5, texture5);
}
}
}
}
}
EditorUtility.SetDirty(replaceMatList[i]);
}
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
tipMsg = "替换成功!替换了" + replaceMatCount + "个Material," + replaceGoList.Count + "个GameObject";
tipMsgType = MessageType.Info;
EditorUtility.ClearProgressBar();
}
void UpdateProgress(int progress, int progressMax, string desc)
{
string title = "Processing...[" + progress + " - " + progressMax + "]";
float value = (float)progress / (float)progressMax;
EditorUtility.DisplayProgressBar(title, desc, value);
}
void Reset()
{
tipMsg = null;
shader = null;
originShader = null;
matCount = 0;
replaceGoList.Clear();
isShowReplaceGo = false;
}
/// <summary>
/// 获取所有场景中的Material
/// </summary>
/// <returns></returns>
Dictionary<GameObject, Material[]> GetAllScenceMaterial()
{
Dictionary<GameObject, Material[]> dict = new Dictionary<GameObject, Material[]>();
List<GameObject> gos = GetAllSceneGameObject();
foreach (var go in gos)
{
Renderer render = go.GetComponent<Renderer>();
if (render != null)
{
Material[] mats = render.sharedMaterials;
if (mats != null && mats.Length > 0 && !dict.ContainsKey(go))
{
dict.Add(go, mats);
matCount += mats.Length;
}
}
}
return dict;
}
/// <summary>
/// 获取所有场景中的物体
/// </summary>
/// <returns></returns>
List<GameObject> GetAllSceneGameObject()
{
List<GameObject> list = new List<GameObject>();
Scene scene = SceneManager.GetActiveScene();
GameObject[] rootGos = scene.GetRootGameObjects();
foreach (var go in rootGos)
{
Transform[] childs = go.transform.GetComponentsInChildren<Transform>(true);
foreach (var child in childs)
{
list.Add(child.gameObject);
}
}
return list;
}
}