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.
A VisualElement that is a drop area needs to register callbacks for the following five event types.
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.
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.
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.
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.
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.
To make a VisualElement draggable, you need to register callbacks on the following three event types.
When the draggable VisualElement receives a MouseDownEvent
, it needs to set its state as “ready for dragging”.
When the draggable VisualElement receives a MouseUpEvent
, it needs to reset its state.
When the draggable VisualElement receives a MouseMoveEvent
and it is ready for dragging, it needs to:
DragAndDrop
.DragAndDrop.StartDrag()
.DragPerformEvent
or a DragExitedEvent
.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.