- Home /
Revert Prefab property override through script
Hi Guys,
I have a situation where materials leaked into the scene due to something I did. But all those materials are in a prefab. Now I want to write an editor script that will find all the materials instances in MeshRenderer.sharedMaterials array in each mesh in the prefab and revert only that particular array element. I tried PrefabUtility.RevertPropertyOverride since I cannot revert the whole object or component or even the entire sharedMaterials array. Only the elements with material instances in it. The problem with PrefabUtility.RevertPropertyOverride is that it takes a SerializedProperty and I'm unable to get the SerializedProperty of the sharedMaterials array or individual elements in it because its a property and not a variable.
Any help would be really appreciated!
Thanks
If you have changed the material asset there is nothing you could do. It is saved in its asset and it is not a part of some prefab's property. As a prefab property, you have only a reference to the material asset, thus you can revert to point again to some asset or to be null for example, but not to change something that is a property of material itself. In other words, material own properties are serialized in its own asset and not into prefab asset.
Yes I know. But my issue is different. $$anonymous$$y materials got leaked into the scene since I used $$anonymous$$eshRenderer.materials ins$$anonymous$$d of $$anonymous$$eshRenderer.shared$$anonymous$$aterials in Editor mode. Its only meant to be used in run time. Now I can sit and revert individual array elements by right-clicking any revert property, but I have over 10k meshes in a scene and 9 scenes. So I just want to write an editor script to do the same.
Hmm, you could search for each mesh if there is an original material asset and set sharedmat = ref asset ins$$anonymous$$d of (instance). Although I can not imagine what criteria you can use in search.
Answer by Marc-Saubion · Feb 13, 2021 at 03:13 PM
Similar issue.
I'd like some renderers to blink red to add some visual cues but don't want to override the prefab material with the original material once it done.
Did you find anything?
Answer by Meceka · Mar 25 at 04:34 PM
I had a similar case and I found a solution myself but it could have some unnecessary lines in the code for your use.
In a foreach loop of all instances of my type called WpNode, I want to revert the property "radius" if its value is 10.
foreach (WpNode node in FindObjectsOfType<WpNode>()) {
GameObject go = node.gameObject;
if (PrefabUtility.GetPrefabInstanceStatus(go)!=PrefabInstanceStatus.Connected) continue;
if (!PrefabUtility.IsOutermostPrefabInstanceRoot(go)) continue;
if (!PrefabUtility.HasPrefabInstanceAnyOverrides(go, false)) continue;
foreach (ObjectOverride objectOverride in PrefabUtility.GetObjectOverrides(go)) {
Object obj = objectOverride.instanceObject;
if (obj.GetType() == typeof(WpNode)) {
WpNode wpNode = (WpNode)obj;
if (wpNode.radius == 10) {
RevertPrefabPropertyOverrideWithMatchingName(obj,"radius");
}
}
}
}
And the function to revert a particular property;
public static void RevertPrefabPropertyOverrideWithMatchingName(Object obj, string name) {
SerializedObject serializedObject = new SerializedObject(obj);
SerializedProperty serializedProperty = serializedObject.FindProperty(name);
PrefabUtility.RevertPropertyOverride(serializedProperty,InteractionMode.AutomatedAction);
Debug.Log("Reverted Object: "+obj.name,obj);
}
Your answer
Follow this Question
Related Questions
Is there a way to prevent non-prefab objects from being linked in the inspector? 0 Answers
How exactly are prefabs tagged with PrefabAssetType enums? 0 Answers
Can anyone help with creating Prefabs for procedural meshes 1 Answer
Cloning a GameObject with Linked Prefab Children 1 Answer
cannot load GameObject Prefabs using Resources.Load in Android 0 Answers