private static bool ReassignMaterial(Object obj, string oldMaterialName, string newMaterialPath) { var result = true; // do the reflection to make sure we get the right material reference if (obj is GameObject go) { foreach (var key in MaterialReferenceBuilder.GetComponentTypes()) { var components = go.GetComponentsInChildren(key); foreach (var component in components) { result &= ReassignMaterialOnComponentOrObject(component, oldMaterialName, newMaterialPath); } } } else { result &= ReassignMaterialOnComponentOrObject(obj, oldMaterialName, newMaterialPath); } return(result); }
public override void OnRun(ref RunItemContext ctx) { var obj = LoadObject(ref ctx); var result = true; var errorString = new StringBuilder(); if (obj != null) { var materials = MaterialReferenceBuilder.GetMaterialsFromObject(obj); foreach (var material in materials) { // there might be multiple materials on this object, we only care about the ones we explicitly try to remap that fail if (!MaterialReferenceBuilder.GetIsReadonlyMaterial(material)) { continue; } if (!ReadonlyMaterialMap.Map.ContainsKey(material.name)) { continue; } if (!ReassignMaterial(obj, material.name, ReadonlyMaterialMap.Map[material.name])) { result = false; errorString.AppendLine($"Material {material.name} failed to be reassigned"); } } } else { result = false; errorString.AppendLine($"Object {ctx.item.descriptor.name} could not be loaded"); } if (!result) { ctx.didFail = true; ctx.info = errorString.ToString(); } else { // make sure the changes get saved EditorUtility.SetDirty(obj); var currentScene = SceneManager.GetActiveScene(); EditorSceneManager.SaveScene(currentScene); } }