Search Unity

Undo and Prefabs

Discussion in 'Prefabs' started by CDF, Jul 18, 2019.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    750
    Hey, so recently discovered that you can no longer just record undo on objects anymore with the new prefab system. Just wanted to get some clarification on best practices with Undo and prefabs.

    To perform a simple change and record, I believe this is all you need:
    Code (CSharp):
    1. EditorGUI.BeginChangeCheck();
    2.  
    3. var newValue = EditorGUI.DoSomeGUIControl(targetObject.value);
    4.  
    5. if (EditorGUI.EndChangeCheck()) {
    6.  
    7.     Undo.RecordObject(targetObject, "Set Value");
    8.  
    9.     targetObject.value = newValue;
    10.  
    11.     PrefabUtility.RecordPrefabInstancePropertyModifications(targetObject);
    12. }
    What I don't understand is do checks need to be put in place to determine if target object is a prefab, prefab instance or non-prefab? It would appear not, but want to make sure.

    Also, what about:
    1. Adding child game objects?
    2. Adding components?
    3. Removing child game objects?
    4. Removing components?
    And other methods of Undo like:
    1. Undo.RegisterCompleteObjectUndo
    2. Undo.RecordObjects
    3. Undo.RegisterCreatedObjectUndo
    4. Undo.RegisterFullObjectHierarchyUndo
    It would seem like there's a requirement for lots of branching code checking if object is prefab, instance or regular game object, but not sure.

    Would be super awesome to get a blog post or something about how to handle Undo with new prefabs. Like comparing the old Undo style with the new. Outlining new approaches to Undo and any possible pitfalls / limitations.

    For the most part it's a non issue, because we can use SerializedObjects. But in some cases you can't, like modifying objects in OnSceneGUI.
     
    Xarbrough likes this.