Search Unity

Undo does not working after Breaking Prefab instance from Script

Discussion in 'Editor & General Support' started by maltakereuz, Oct 6, 2018.

  1. maltakereuz

    maltakereuz

    Joined:
    Mar 29, 2015
    Posts:
    53
    I breaking prefab instances pretty often. So i want to have a shortcut for it and wrote a simple script. It works fine, but undo does not work, even with Undo.RecordUndo(obj, "history name"). How can i improve this script, to make undo work?

    Here is the script:

    Code (CSharp):
    1. [MenuItem("MyGameTools/Unconnect prefab (break) %u")]
    2. static void UnconnectPrefab() {
    3.     string msg = "";
    4.     if (Selection.gameObjects.Any()) {
    5.         foreach (var go in Selection.gameObjects) {
    6.             Undo.RecordObject(go, "disconnect prefab " + go);
    7.             PrefabUtility.DisconnectPrefabInstance(go);
    8.             EditorUtility.SetDirty(go);
    9.             msg += go.name + " – disconnect prefab\n";
    10.         }
    11.     } else {
    12.         msg = "No objects seleted to disconnect prefab";
    13.     }
    14.     Debug.Log(msg);
    15. }
    Alternativly any method to map editor's "Break Prefab Instance" to hotkey would work for me too, because undo works there out of the box.
     
  2. maltakereuz

    maltakereuz

    Joined:
    Mar 29, 2015
    Posts:
    53
    Oh, i think i found solution, calling this before register undo works:
    Code (CSharp):
    1. Undo.RegisterCompleteObjectUndo(go, "disconnect prefab " + go);