Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How can I undo with drag and drop in the treeview?

Discussion in 'Editor & General Support' started by kbop2000, Apr 8, 2021.

  1. kbop2000

    kbop2000

    Joined:
    Apr 17, 2019
    Posts:
    14
    Code was written to implement undo in drag and drop while implementing treeview
    This code is TreeView Sample example code

    Code (CSharp):
    1. protected override void SetupDragAndDrop(SetupDragAndDropArgs args)
    2.         {
    3.             DragAndDrop.PrepareStartDrag();
    4.  
    5.             var sortedDraggedIDs = SortItemIDsInRowOrder(args.draggedItemIDs);
    6.             GameObject[] gameObjects = new GameObject[sortedDraggedIDs.Count];
    7.             for (int i = 0; i < gameObjects.Length; ++i)
    8.                 gameObjects[i] = GetGameObject(sortedDraggedIDs[i]);
    9.  
    10.             DragAndDrop.objectReferences = gameObjects;
    11.  
    12.             Undo.RecordObjects(gameObjects, "Dragging GameObjects");
    13.             DragAndDrop.StartDrag("Dragging GameObjects");
    14.         }
    Code (CSharp):
    1. protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
    2.         {
    3.             var draggedObjects = DragAndDrop.objectReferences;
    4.             var transforms = new List<Transform>(draggedObjects.Length);
    5.             foreach (var obj in draggedObjects)
    6.             {
    7.                 var go = obj as GameObject;
    8.                 if (go == null)
    9.                 {
    10.                     return DragAndDropVisualMode.None;
    11.                 }
    12.  
    13.                 transforms.Add(go.transform);
    14.             }
    15.  
    16.             RemoveItemsThatAreDescendantsFromOtherItems(transforms);
    17.  
    18.             // Reparent
    19.             if (args.performDrop)
    20.             {
    21.                 Transform root = GameObject.Find("Game Root").transform;
    22.  
    23.                 switch (args.dragAndDropPosition)
    24.                 {
    25.                     case DragAndDropPosition.UponItem:
    26.                     case DragAndDropPosition.BetweenItems:
    27.                         Transform parent = args.parentItem != null ? GetGameObject(args.parentItem.id).transform : root;
    28.  
    29.                         if (!IsValidReparenting(parent, transforms))
    30.                             return DragAndDropVisualMode.None;
    31.  
    32.                         foreach (var trans in transforms)
    33.                             trans.SetParent(parent);
    34.  
    35.                         if (args.dragAndDropPosition == DragAndDropPosition.BetweenItems)
    36.                         {
    37.                             int insertIndex = args.insertAtIndex;
    38.                             for (int i = transforms.Count - 1; i >= 0; i--)
    39.                             {
    40.                                 var transform = transforms[i];
    41.                                 insertIndex = GetAdjustedInsertIndex(parent, transform, insertIndex);
    42.                                 transform.SetSiblingIndex(insertIndex);
    43.                             }
    44.                         }
    45.                         break;
    46.  
    47.                     case DragAndDropPosition.OutsideItems:
    48.                         foreach (var trans in transforms)
    49.                         {
    50.                             trans.SetParent(root); // make root when dragged to empty space in treeview
    51.                         }
    52.                         break;
    53.  
    54.                     default:
    55.                         throw new ArgumentOutOfRangeException();
    56.                 }
    57.  
    58.                 Reload();
    59.             }
    60.  
    61.             return DragAndDropVisualMode.Move;
    62.         }
    But it doesn't work like the image below

    upload_2021-4-8_18-59-19.png

    can i get help?
    thank you
     
  2. kbop2000

    kbop2000

    Joined:
    Apr 17, 2019
    Posts:
    14
    My mistake ..
    It works well with Undo.SetTransformParent() function in it
    https://docs.unity3d.com/ScriptReference/Undo.SetTransformParent.html

    Code (CSharp):
    1. case DragAndDropPosition.OutsideItems:
    2.                         foreach (var trans in transforms)
    3.                         {
    4.                             Undo.SetTransformParent(trans, root, "Dragging GameObjects");
    5.                             trans.SetParent(root);
    6.                         }
    7.                         break;