135 lines
4.1 KiB
C#
135 lines
4.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public class ReplacePrefabMaterials : EditorWindow
|
|
{
|
|
private GameObject targetGameObject;
|
|
private Material materialToFind;
|
|
private Material materialToReplace;
|
|
|
|
[MenuItem("Tools/Replace Prefab Materials")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<ReplacePrefabMaterials>("Replace Prefab Materials");
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
GUILayout.Label("Replace Materials in Prefabs", EditorStyles.boldLabel);
|
|
|
|
targetGameObject = (GameObject)EditorGUILayout.ObjectField("Target Game Object", targetGameObject, typeof(GameObject), true);
|
|
|
|
materialToFind = (Material)EditorGUILayout.ObjectField("Material to Find", materialToFind, typeof(Material), false);
|
|
materialToReplace = (Material)EditorGUILayout.ObjectField("Material to Replace", materialToReplace, typeof(Material), false);
|
|
|
|
if (GUILayout.Button("Replace Selected Prefabs"))
|
|
{
|
|
ReplaceTargetObjectMaterials();
|
|
}
|
|
|
|
if (GUILayout.Button("Replace in All Prefabs"))
|
|
{
|
|
ReplaceAllPrefabMaterials();
|
|
}
|
|
}
|
|
|
|
private void ReplaceTargetObjectMaterials()
|
|
{
|
|
if (materialToFind == null || materialToReplace == null)
|
|
{
|
|
Debug.LogError("Please assign both materials.");
|
|
return;
|
|
}
|
|
|
|
if (targetGameObject == null)
|
|
{
|
|
Debug.LogError("Please assign the target GameObject.");
|
|
return;
|
|
}
|
|
|
|
Renderer[] renderers = targetGameObject.GetComponentsInChildren<Renderer>(true);
|
|
bool isDirty = false;
|
|
int count = 0;
|
|
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] materials = renderer.sharedMaterials;
|
|
for (int i = 0; i < materials.Length; i++)
|
|
{
|
|
if (materials[i] == materialToFind)
|
|
{
|
|
materials[i] = materialToReplace;
|
|
isDirty = true;
|
|
count++;
|
|
}
|
|
}
|
|
if (isDirty)
|
|
{
|
|
renderer.sharedMaterials = materials;
|
|
}
|
|
}
|
|
|
|
if (isDirty)
|
|
{
|
|
EditorUtility.SetDirty(targetGameObject);
|
|
Debug.Log(count + "Complete");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"No instances of {materialToFind.name} found in the target GameObject.");
|
|
}
|
|
}
|
|
|
|
private void ReplaceAllPrefabMaterials()
|
|
{
|
|
if (materialToFind == null || materialToReplace == null)
|
|
{
|
|
Debug.LogError("Please assign both materials.");
|
|
return;
|
|
}
|
|
|
|
int count = 0;
|
|
string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab");
|
|
foreach (string prefabGuid in prefabGuids)
|
|
{
|
|
string prefabPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
|
|
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
|
|
|
|
if (prefab != null)
|
|
{
|
|
Renderer[] renderers = prefab.GetComponentsInChildren<Renderer>(true);
|
|
bool isDirty = false;
|
|
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] materials = renderer.sharedMaterials;
|
|
for (int i = 0; i < materials.Length; i++)
|
|
{
|
|
if (materials[i] == materialToFind)
|
|
{
|
|
materials[i] = materialToReplace;
|
|
isDirty = true;
|
|
count++;
|
|
}
|
|
}
|
|
if (isDirty)
|
|
{
|
|
renderer.sharedMaterials = materials;
|
|
}
|
|
}
|
|
|
|
if (isDirty)
|
|
{
|
|
EditorUtility.SetDirty(prefab);
|
|
PrefabUtility.SavePrefabAsset(prefab);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Null");
|
|
}
|
|
}
|
|
|
|
Debug.Log($"Replaced {count} instances of {materialToFind.name} with {materialToReplace.name} in prefabs.");
|
|
}
|
|
} |