Automatically apply prefab changes

HI, can someone help with this?

I would like to have a component that applies the changes done to a prefab instance to the prefab itself, like hitting the apply button each time a change is done. Looks like it should be something done around this utility:

PrefabUtility.SetPropertyModifications

But I’m not knowledgeable enough with the editor to do it myself.

1 Like

You can actually dig around Unity’s code using either MonoDevelop or any .NET decompiler (it’s in the GameObjectInspector class).

When you hit “Apply”, basically it appears to be doing this (roughly):

  • Call PrefabUtility.FindValidUploadPrefabInstanceRoot to get the root object for the prefab.
  • Call PrefabUtility.GetPrefabParent on that, to get back the asset that this prefab is stored in (in the project browser)
  • Call PrefabUtility.ReplacePrefab to store back the prefab data.
  • Finally (appears to be unrelated to hitting Apply), calls serializedObject.ApplyModifiedProperties

All of these methods are exposed publicly and can be called by an editor extension you create.

When do you want this behaviour to fire? with every change you make to the component in the inspector ?

If that’s the case, you should start by creating your own inspector for your component (http://www.tallior.com/unity-custom-inspectors/) that will call the functionality i listed above with every change you make.