onEndDrag, OnDrop - Order of operations?

So I have been looking up tutorials for dragging and dropping objects, and I see people use 2 scripts one for the item being dragged, and a second script for the slots an item can snap to. The expected behaviour is that if the item is not dropped on a slot it snaps back to its original position.

Now in the tutorials people implement it sort of like this:

Draggable item Code:

    public override void OnEndDrag(PointerEventData data)
    {
        transform.position = startPosition;
    }

Slot Code:

 public override void OnDrop(PointerEventData data)
    {
        data.pointerDrag.transform.position = transform.position;
    }

Now this ends up working as expected, if you drop it outside a slot it snaps back to the original pos, if you drop it in a slot it snaps to the slot, my question is why does it happen like this? It seems the order of operations is the OnEndDrag is resolving before the OnDrop, but it all happens in the same frame so the item ends up snapped to the slot.

Am I correct in assuming this or am I missing something, is the OnEndDrag just not triggering? Thanks!

any insights?

Simple enough to throw Debug.Log statements in there to find out no?

Looks like in the StandardInputModule, the mouse events are fired in the order of: up, click, drop, end drag. It uses the same order for touch input as well.

1 Like