Search Unity

PrefabUtility.UnpackPrefabInstance deslects other objects

Discussion in 'Prefabs' started by CDF, Aug 22, 2019.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    So I have a problem. PrefabUtility.UnpackPrefabInstance seems to deselect current selection (and thus destroy active Editors, which in turn Disposes serializedObjects)

    This is a problem.
    Especially when you want to write a DragAndDrop handler for an Editor that might need to Unpack the dragged object during processing of the drag. Here's what's going on:

    Code (CSharp):
    1.  
    2. void OnEnable() {
    3.  
    4.     list = serializedObject.FindProperty("list");
    5. }
    6.  
    7. void OnDisable() {
    8.  
    9.     Debug.Log("Hi, I got disabled during drag and drop, isn't that fun!");
    10. }
    11.  
    12. public override void OnInspectorGUI() {
    13.  
    14.     serializedObject.Update();
    15.  
    16.     HandleDragAndDrop();
    17.  
    18.     serializedObject.ApplyModifiedProperties();
    19. }
    20.  
    21. void HandleDragAndDrop() {
    22.  
    23.     //drag and drop code ommited
    24.  
    25.     //process the drag and drop object "instance"
    26.  
    27.     if (PrefabUtility.IsAnyPrefabInstanceRoot(instance)) {
    28.  
    29.         //This method causes Unity to deselect the current selection. Causing the serializedObject on Editor to be disposed!
    30.  
    31.         PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
    32.     }
    33.  
    34.     //save instance as prefab
    35.  
    36.     bool success;
    37.     GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(instance, assetPath, InteractionMode.AutomatedAction, out success);
    38.  
    39.     //add instance to list *ERROR* - NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
    40.  
    41.     list.GetArrayElementAtIndex(dragAndDropIndex).objectReferenceValue = instance;
    42. }
    43.  
    Would be super great if it did not change Selection, or dispose current Editor after Unpacking.