How to Unity

Drag And Drop UI Objects in Unity

Use IDragHandler and IDropHandler to implement a drag-and-drop feature in Unity.

Daniele Quero, PhD
Nerd For Tech
Published in
4 min readJun 12, 2024

--

As seen in this previous article, some interfaces can be implemented by a MonoBehaviour script in Unity to have pointer-related interaction involving UI elements.

In this article, I will show a couple more, about the well-known drag-and-drop feature. Namely, I will focus on:

which you can explore better in the Official Scripting API manual. Each interface exposes a method, respectively:

OnEndDrag(PointerEventData pointerEventData)
OnBeginDrag(PointerEventData pointerEventData)
OnDrag(PointerEventData pointerEventData)
OnDrop(PointerEventData pointerEventData)

Quite straightforward: what happens when the drag operation ends, begins, is being performed, and what happens when what’s been dragged is dropped.

Let’s look at a use case: dragging items around and dropping them into slots.

--

--

Daniele Quero, PhD
Nerd For Tech

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams