Drag events are sent during operations where visual elementsA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary have drag-and-drop behavior. This is an Editor-only event.
To implement drag-and-drop functionality, make sure that visual elements register callbacks for specific events.
Visual elements that support drag operations separate into two types:
You can select a draggable visual element, drag it to a droppable visual element, and release the element to drop it.
The base class for all drag-and-drop events is DragAndDropEventBase.
Event | Description | Trickles down | Bubbles up | Cancellable |
---|---|---|---|---|
DragExitedEvent | Sent when the drag-and-drop process ends. | ✔ | ✔ | |
DragUpdatedEvent | Sent when the dragged element moves over a drop target. | ✔ | ✔ | ✔ |
DragPerformEvent | Sent when the dragged element drops onto a target. | ✔ | ✔ | ✔ |
DragEnterEvent | Sent when the dragged element enters a new VisualElement . |
✔ | ||
DragLeaveEvent | Sent when the dragged element exits the current drop target. | ✔ |
To make a visual element draggable, you need to register callbacks on the following three event types:
Use the following steps for a drag operation:
DragAndDrop
.DragAndDrop.StartDrag()
.DragPerformEvent
or a DragExitedEvent
.The DragExitedEvent
is sent when the user drags any draggable object over a visual element and releases the mouse pointer. When a drop area visual element receives a DragExitedEvent
, it needs to remove all feedback from drag operations.
The DragUpdatedEvent
is sent when the pointer moves over a visual element as the user moves a draggable object.
When a drop area visual element receives a DragUpdatedEvent
, it needs to update the drop feedback. For example, you can move the “ghost” of the dragged object so it stays under the mouse pointer.
The drop area visual element 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, or reject the drop operation.
The DragPerformEvent
is sent when the user drags any draggable object and releases the mouse pointer over a visual element. This only occurs if a visual element sets DragAndDrop.visualMode
to something other than DragAndDropVisualMode.None
or DragAndDropVisualMode.Rejected
to indicate that it can accept dragged objects.
When a drop area visual element receives a DragPerformEvent
, it needs to act on the dragged objects stored in DragAndDrop.objectReferences
, DragAndDrop.paths
or DragAndDrop.GetGenericData()
.
For example, it might add new visual elements at the location where the user drops the objects.
The DragEnterEvent
is sent when the pointer enters a visual element during a drag operation.
When a drop area visual element 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. For example, you can add a USS class to the target element and display a “ghost” of the dragged object under the mouse pointer.
The DragLeaveEvent
is sent when the pointer exits a visual element as the user moves a draggable object.
When a drop area visual element receives a DragLeaveEvent
, it needs to stop providing drop feedback. For example, you can remove the USS class that you added when the target element received a DragEnterEvent
, and no longer display the “ghost” of the dragged object.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.