Prevent IDragHandler from also triggering PointerClickHandler?

Hello! I’m working on an inventory script and having a bit of trouble with the IPointerClickHandler and the IDragHandler. Currently, my code executes everything in OnDrag() and OnEndDrag() properly, but when i let go of an item, it also registers a normal LMB click. How to prevent this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemClickHandler : MonoBehaviour, IDragHandler, IEndDragHandler, IPointerClickHandler
{
    public Transform itemMenu;
    public Transform inventory;
    public Transform slot;

    //Store currently dragged item

    public void OnDrag(PointerEventData dragData)
    {
        transform.position = Input.mousePosition;
        transform.SetParent(inventory);
        transform.SetAsLastSibling();
    }

    public void OnEndDrag(PointerEventData endDragData)
    {
        transform.SetParent(slot);
        transform.SetAsLastSibling();
        transform.localPosition = Vector3.zero;
        
        //assign currently dragged item to the inventory slot array it was dropped on and put old otem in the old slot
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            Debug.Log("Using item in " + name);
            //USE ITEM
        }
        else if (eventData.button == PointerEventData.InputButton.Middle)
        {
            //if item associated with inventory slot isStackable, split stack.
            //change split size with mouse wheel
        }
        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            Debug.Log("Opening item menu for " + name);
            //OPEN ITEM MENU
        }
    }
}

Since I ran into this problem myself and this is the first thing that pops up on Google, I thought I’d share my solution. If the click happens at the end of the drag, PointerEventData.dragging returns true, so you can just check for that and return. Here’s my code:

public void OnPointerClick(PointerEventData eventData) {
    if (eventData.dragging) {
        return;
    }
    switch(eventData.button) {
        case PointerEventData.InputButton.Left:
            Rotate(1);
            return;
        case PointerEventData.InputButton.Right:
            Rotate(3);
            return;
        default:
            return;
    }
}

That does seem silly that OnPointerClick is executed after releasing the mouse button after a drag.


You can detect if you dragged the mouse or not by evaluating the distance between when you pressed the mouse button and when you released it.

float dragDistance = Vector2.Distance( eventData.pressPosition, eventData.position );
float dragThreshold = 10f;
bool isDrag = dragDistance > dragThreshold ;

if( isDrag )
     return;

Update!

KillHour 's newer answer below is more elegantly handled. I recommend that approach instead of this.