Version: 2020.2
Responding to Events
Synthesizing Events

Supporting drag and drop

To implement drag and drop functionality, you need to make sure that drop area VisualElements and draggable VisualElements register callbacks for specific events. This page discusses what occurs when these VisualElements receive events.

For more information about events, see the documentation on The Event System.

Registering callbacks for drop areas

A VisualElement that is a drop area needs to register callbacks for the following five event types.

DragEnterEvent

DragEnterEvent is sent when the pointer enters a VisualElement as the user drags a draggable object.

When a drop area VisualElement receives a DragEnterEvent, it needs to provide feedback that lets the user know that it, or one of its children, is a target for a potential drop operation.

You can do this by, for example, adding a USS class to the target element, and also displaying a “ghost” of the dragged object under the mouse pointer.

DragLeaveEvent

The DragLeaveEvent is sent when the pointer exits a VisualElement as the user drags a draggable object.

When a drop area VisualElement receives a DragLeaveEvent, it needs to stop providing drop feedback.

You can do this by, for example, removing the USS class that you added when the target element received a DragEnterEvent, and no longer displaying the “ghost” of the dragged object.

DragUpdatedEvent

The DragUpdatedEvent is sent when the pointer moves over a VisualElement as the user drags a draggable object.

When a drop area VisualElement receives a DragUpdatedEvent, it needs to update the drop feedback.

You can do this by, for example, moving the “ghost” of the dragged object so it stays under the mouse pointer.

The drop area VisualElement should also examine DragAndDrop properties and set DragAndDrop.visualMode to indicate the effect of a drop operation. For example, a drop operation could create a new object, move an existing object, reject the drop operation, and so on.

DragPerformEvent

The DragPerformEvent is sent when the user drags any draggable object and releases the mouse pointer over a VisualElement. This only occurs if a VisualElement sets DragAndDrop.visualMode to something other than DragAndDropVisualMode.None or DragAndDropVisualMode.Rejected to indicate that it can accept dragged objects.

When a drop area VisualElement receives a DragPerformEvent, it needs to take appropriate action on the dragged objects stored in DragAndDrop.objectReferences, DragAndDrop.paths or DragAndDrop.GetGenericData().

For example, it might add new VisualElements at the location where the user drops the objects.

DragExitedEvent

The DragExitedEvent is sent when the user drags any draggable object over a VisualElement and releases the mouse pointer. This only occurs if no VisualElement indicates that it can accept the dragged objects.

When a drop area VisualElement receives a DragExitedEvent, it needs to remove all drag and drop feedback.

Note: there is currently a bug in UI(User Interface) Allows a user to interact with your application. More info
See in Glossary
Toolkit that prevents DragExitedEvent from being sent. As a workaround, you can implement the relevant functionality in DragLeaveEvent, which is sent when you stop a drag and drop operation.

Making VisualElements draggable

To make a VisualElement draggable, you need to register callbacks on the following three event types.

MouseDownEvent

When the draggable VisualElement receives a MouseDownEvent, it needs to set its state as “ready for dragging”.

MouseUpEvent

When the draggable VisualElement receives a MouseUpEvent, it needs to reset its state.

MouseMoveEvent

When the draggable VisualElement receives a MouseMoveEvent and it is ready for dragging, it needs to:

  • Set its state to “being dragged”.
  • Add the appropriate data to DragAndDrop.
  • Call DragAndDrop.StartDrag().
  • Provide visual feedback to show that it is the object of a drag operation.

    The drop area VisualElement should remove this feedback when it receives a DragPerformEvent or a DragExitedEvent.
Responding to Events
Synthesizing Events