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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Performing Drag on VisualElement

Discussion in 'UI Toolkit' started by cursedtoast, Sep 15, 2022.

  1. cursedtoast

    cursedtoast

    Joined:
    Apr 27, 2018
    Posts:
    62
    Hello! I'm trying to build out an EditorWindow that allows you to drag a label (without actually moving it) to object field slots. When you start dragging the label, and GameObject type is populated.

    I was trying to follow along with instructions on this doc page

    The problem:
    Everything works well, but I can't seem to figure out how to end the drag when dropping into another window. I end up getting console errors, even after I've stopped dragging:
    Code (CSharp):
    1. Drags can only be started from MouseDown or MouseDrag events
    Here's my current setup, hoping someone can give me direction so I can wrap this feature up:

    Code (CSharp):
    1.  
    2.         dragLabel.RegisterCallback<MouseDownEvent>(OnMouseDown);
    3.         dragLabel.RegisterCallback<MouseUpEvent>(OnMouseUp);
    4.         dragLabel.RegisterCallback<MouseMoveEvent>(OnMouseMove);
    5.  
    6. ...
    7. ...
    8. ...
    9.  
    10.     private void OnMouseDown(MouseDownEvent evt)
    11.     {
    12.         dragReady = true;
    13.         DragAndDrop.PrepareStartDrag();
    14.         DragAndDrop.SetGenericData(interaction.name, interactionObject);
    15.         DragAndDrop.objectReferences = new UnityEngine.Object[] { interactionObject };
    16.         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
    17.     }
    18.    
    19.     private void OnMouseUp(MouseUpEvent evt)
    20.     {
    21.         dragReady = false;
    22.     }
    23.    
    24.     private void OnMouseMove(MouseMoveEvent evt)
    25.     {
    26.         if (dragReady)
    27.         {
    28.             DragAndDrop.StartDrag(interaction.name);
    29.         }
    30.     }
    Something keeps setting the value of dragReady back to true, even if I'm not clicking an element. Any guidance would be greatly appreciated, thank you!
     
  2. pierre_10

    pierre_10

    Unity Technologies

    Joined:
    Apr 1, 2016
    Posts:
    33
    Try adding a DRagExitedEvent
     
    cursedtoast likes this.