Helena
Helena

Reputation: 1067

Unity EventTrigger steals OnDrop but not OnDrag

I have written my own component in Unity, which implements IBeginDragHandler, IDragHandler and IDropHandler. I want to add an EventTrigger component that comes with UnityEngine.UI, but when I add it I stop getting calls to OnDrop on my component. OnBeginDrag and OnDrag are called as usual.

My component code:

public class MyComponent : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag!");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag!");
    }

    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("OnDrop!");
    }
}

When I start my scene with a Game object with this component attached, the expected log output is:

OnBeginDrag!
OnDrag!
...
OnDrag!
OnDrop!

This is the case as long as I only have default components and my component. But if I add an Event -> Event Trigger component (without even specifying anything in it) to the same GameObject and run again, the output is:

OnBeginDrag!
OnDrag!
...
OnDrag!

The OnDrop is never called. The order of the components doesn't affect anything.

Is there a way to stop EventTrigger from using up the OnDrop call?

Upvotes: 2

Views: 2379

Answers (1)

Kardux
Kardux

Reputation: 2157

Well this has been here for a long time (I can't remember when I found it too but at least 1.5-2 years ago)...

A workaround is to use OnEndDrag event as it's always called (without an EventTrigger component it get called right after OnDrop). Also it makes sens that both those events symbolize quite the same thing: an item won't be dropped if it hasn't started being dragged beforehand.

Otherwise you could "copy" the EventTrigger class behaviour in your own script with the possibility of assigning a GameObject and a Method (and even Parameters) that will be called when OnDrop is called so you won't have to add an EventTrigger component.

Hope this helps,

Upvotes: 1

Related Questions