This is a follow up to this question
Assume I have a simple MonoBehaviour
public class FooScript : MonoBehaviour {
public int someValue = 0;
public int otherValue = 0;
}
I put that on an empty GameObject then drag the GameObject to the Project to create a prefab.
If I select the prefab and edit someValue
then if I go to the instance still in the scene I see someValue
is updated.
If I edit someValue
on the instance then update someValue
on the prefab someValue
is NOT updated on the instance but if I update otherValue
on the prefab it does update on the instance. In other words each instance property has it's own flag on if it has been modified from the source prefab or not.
But, if I make an editor script and lookup FooScript
on the prefab and set someValue
the instance is NOT updated, only the prefab. I want it to act exactly as if I had done it manually in the editor. In other words, if the instance property has been edited by the user don't update. If instance property has not been edited do update.
The question is what's the correct way to do that? Looking at PrefabUtilily
there doesn't appear to be a ApplyPropertiesFromSourcePrefabOnAllUnmodifiedInstancePropreties
function.
The only solution I can think of sounds EXTREMELY convoluted. I'd have to
- find all instances (walk the entire hierarchy, not sure what happens in unloaded scenes)
- for each instance record the modifications are on it by calling
PrefabUtility.GetPropertyModifications
and storing the results in someDictionary<GameObject, PropertyModifications[]>
. (lots of memory) - Apply my change to the prefab.
- Then, for every instance that doesn't have a modification to the field I'm updating update the that property on instance as well.
Is it really that convoluted or is there some easier way?
PrefabUtility.MergeAllPrefabInstances()
? – Isaac van BakelEditorUtilitiy.SaveDirty(prefabGameobject)
, and that also worked which seems more appropriate? – gmanEditorUtility.SetDirty(Object)
? The docs for that also say that you shouldn't be using it. – Isaac van Bakel