Performing Drag on VisualElement

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:

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:

        dragLabel.RegisterCallback<MouseDownEvent>(OnMouseDown);
        dragLabel.RegisterCallback<MouseUpEvent>(OnMouseUp);
        dragLabel.RegisterCallback<MouseMoveEvent>(OnMouseMove);

...
...
...

    private void OnMouseDown(MouseDownEvent evt)
    {
        dragReady = true;
        DragAndDrop.PrepareStartDrag();
        DragAndDrop.SetGenericData(interaction.name, interactionObject);
        DragAndDrop.objectReferences = new UnityEngine.Object[] { interactionObject };
        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
    }
   
    private void OnMouseUp(MouseUpEvent evt)
    {
        dragReady = false;
    }
   
    private void OnMouseMove(MouseMoveEvent evt)
    {
        if (dragReady)
        {
            DragAndDrop.StartDrag(interaction.name);
        }
    }

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!

Try adding a DRagExitedEvent

1 Like