PrefabUtility.ConnectGameObjectToPrefab with Undo

How can I make Unity - Scripting API: PrefabUtility.ConnectGameObjectToPrefab work with Undo?

I’ve tried Undo.RecordObject, but I’v soon noticed, that the PrefabUtility actually destroys the previous GameObject instance in the scene, then instantiates a new one from the prefab and copies the last instance settings.

Is it possible to record undo steps for this?

I’ve also noticed, that the Transform data is overriden on the new instance, although all other properties seem to be handled correctly (meaning, if they are different from the prefab, they won’t be changed, but show up as bold labels instead). Is this a bug or am I not using the feature correctly?

public GameObject target;
public GameObject prefab;

void MyEditorFunction()
{
    // Undo.RecordObject(target); // Doesn't work, because target will be destroyed.
    // Undo.RegisterFullObjectHierarchyUndo(target); // This even crashes Unity and corrupts the scene file permanently.
    // -> Reported in case 936220
    PrefabUtility.ConnectGameObjectToPrefab(targets, prefab);
}

This is late but just having coming across this issue myself and seeing there’s no answer yet…

I think you need to record the changes and then reapply them. That’s what I’m doing, at least. So something like…

Vector3 pos = target.transform.pos;
target = PrefabUtility.ConnectGameObjectToPrefab(target, prefab);
target.transform.position = pos;

Crucially, because as you say ConnectObjectToPrefab destroys and recreates the object, you need to grab its return value (which is the new object) and use that in place of your previous reference.

ConnectObjectToPrefab is a terrible name for a function that destroys the thing you pass in to it. It would have saved me a day’s work if they’d named it more appropriately.

Any solution yet? Would it be sufficient to destroy the original object, instantiate the prefab (at the same position and parent) and record that action? What about unrelated children objects?