Member-only story
How to Unity
Drag And Drop UI Objects in Unity
Use IDragHandler and IDropHandler to implement a drag-and-drop feature in Unity.
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.