Suggest a change
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
Your name Your email Suggestion*
Cancel
Parameters
eventData | Current event data. |
Description
Called by the EventSystem every time the pointer is moved during dragging.
no example available in JavaScript//Attach this script to the GameObject you would like to detect dragging on //Attach an Event Trigger component to the GameObject (Click the Add Component button and go to Event>Event Trigger) //Make sure the Camera you are using has a Physics Raycaster (Click the Add Component button and go to Event>Physics Raycaster) so it can detect clicks on GameObjects.
using UnityEngine; using UnityEngine.EventSystems;
public class OnDragExample : MonoBehaviour { void Start() { EventTrigger trigger = GetComponent<EventTrigger>(); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.Drag; entry.callback.AddListener((data) => { OnDragDelegate((PointerEventData)data); }); trigger.triggers.Add(entry); }
public void OnDragDelegate(PointerEventData data) { Debug.Log("Dragging."); } }
The next example allows you to move a GameObject by dragging.
no example available in JavaScriptusing UnityEngine; using UnityEngine.EventSystems;
public class OnDragExample : MonoBehaviour { void Start() { //Fetch the Event Trigger component from your GameObject EventTrigger trigger = GetComponent<EventTrigger>(); //Create a new entry for the Event Trigger EventTrigger.Entry entry = new EventTrigger.Entry(); //Add a Drag type event to the Event Trigger entry.eventID = EventTriggerType.Drag; //call the OnDragDelegate function when the Event System detects dragging entry.callback.AddListener((data) => { OnDragDelegate((PointerEventData)data); }); //Add the trigger entry trigger.triggers.Add(entry); }
public void OnDragDelegate(PointerEventData data) { //Create a ray going from the camera through the mouse position Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Calculate the distance between the Camera and the GameObject, and go this distance along the ray Vector3 rayPoint = ray.GetPoint(Vector3.Distance(transform.position, Camera.main.transform.position)); //Move the GameObject when you drag it transform.position = rayPoint; } }
Did you find this page useful? Please give it a rating:
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:
- Draggable visual elements
- Droppable visual elements
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.
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. | ✔ |
Make visual elements draggable
To make a visual element draggable, you need to register callbacks on the following three event types:
- PointerDownEvent
- PointerUpEvent
- PointerMoveEvent
Use the following steps for a drag operation:
- Set its state to “being dragged”.
- Add the appropriate data to DragAndDrop.
- Call DragAndDrop.StartDrag().
- Provide a visual cue to the drag operation. The drop area visual element should remove this feedback when it receives a DragPerformEvent or a DragExitedEvent.
Event list
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.
DragUpdatedEvent
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.
DragPerformEvent
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.
DragEnterEvent
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.
DragLeaveEvent
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.
Examples
- Create a drag-and-drop UI
- Create a drag-and-drop UI to drag between Editor windows