- Home /
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
}
}
}
Answer by Jonathan-Bro · Sep 27, 2018 at 06:35 AM
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.
Thank you, i figured I'd have to go with something like this but for some reason distance wasnt co$$anonymous$$g to $$anonymous$$d and I was attempting to build it off click time, which was a bit awkward.
Unfortunately, the drag handler is a bit too clunky for my UI elements and slow initial response to the drag is causing some bugs, so I think I'll just need to build my own. Thanks though
Answer by KillHour · Jan 14 at 02:14 AM
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612175334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Button Listener void calling all prefab buttons in scene. 1 Answer
OnDrag only called once? 1 Answer
Draggable items in inventory 1 Answer
onMouseEnter not called while dragging 0 Answers