Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by $$anonymous$$ · Sep 27, 2018 at 02:15 AM · inventoryeventsystemdrag and drop

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
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Sep 30, 2018 at 06:54 AM 0
Share

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

avatar image
3

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button Listener void calling all prefab buttons in scene. 1 Answer

Draggable items in inventory 1 Answer

OnDrag only called once? 1 Answer

onMouseEnter not called while dragging 0 Answers

How to Simplify My Drag and Drop Inventory 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges