- Home /
Apply changes on asset to instance
I have a custom editor displaying some properties of my custom class "Room" in the inspector.
Whenever I change one property on the asset all the instances are updated automatically. Unless of course I have overriden that specific property on the instance. Now I would like to make sure that whenever I change the property on the asset, all instances are updated regardless of whether they are overriden or not. But I don't know how to find all the respective instances in the scene in order to change them. So my question is how does Unity do it? It needs to find all instances and update them somehow once an asset is changed. And how could I do it?
Thank you for your help.
Answer by DiegoSLTS · Apr 19, 2019 at 03:07 PM
I guess you could use Object.FindObjectsOfType() to get all objects with that component, then use PrefabUtility methods.
You can make sure the object is an instance of the prefab you're editing using GetCorrespondingObjectFromSource. You can make sure the instance is still connected to the prefab with IsDisconnectedFromPrefabAsset.
Once you confirmed it's a connected instance of the prefab you can change the value, but you probably need to get the serializedObject for that instance if you want the changes to be persisted: https://docs.unity3d.com/ScriptReference/SerializedObject.html
Anyway, all this sounds like a really bad idea, you'll be doing exactly the opposite of what prefab overrides are intended to do. It might be better if you make a script that checks all prefab instances of the scene on reverts the properties that are not supposed to be overriden. Check other methods of PrefabUtility, like: https://docs.unity3d.com/ScriptReference/PrefabUtility.RevertPropertyOverride.html
With this approach you can run the script when you open a scene and it'll "fix" any prefab instance overriding those values on the scene. The other solution will only work on objects on currently open scenes.
Thank you for the detailed answer. It's all I was looking for. You are right. I am following a wrong path. Going against the system. RevertPropertyOverride seems like a better solution. Thanks again!!!
Your answer
Follow this Question
Related Questions
Custom Editor serializing data in prefab instance. 0 Answers
Cost of loading manually modified instances at runtime, on mobiles? 1 Answer
Nested ScriptableObject assets? 1 Answer
Object references in a custom editor 2 Answers
Is there a way to override a function in each instance of the same script? 3 Answers