• 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
4
Question by TimGS · Oct 15, 2015 at 07:17 AM · uieventsunity 5.2

Mouse drag element inside ScrollRect throws PointerUp event.

Inside ScrollRect I have UI elements with Monobehaviour scripts which implement OnPointerDown() and OnPointerUp() methods. OnPointerDown() locks scrolling and OnPointerUp() unlocks it.

OnPointerDown() works as intended. It's called when I click the element. But when I try to move the mouse while holding mouse button down it immediately throws OnPointerUp event and my OnPointerUp() method is called.

Is it supposed to be so? Shouldn't OnPointerUp be thrown when I release the mouse button?

Unity 5.2.1p4

Comment
Add comment · Show 4
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 TimGS · Oct 22, 2015 at 09:51 PM 0
Share

No answer so far. I submitted a bug report.

avatar image meat5000 ♦ TimGS · Oct 22, 2015 at 11:41 PM 0
Share

Care to post an example project?

Did you implement IDragHandler?

avatar image TimGS meat5000 ♦ · Oct 23, 2015 at 12:42 AM 0
Share

I didn't implement IDragHandler. Should I? I just implemented IPointerDownHandler and IPointerUpHandler.

Here's an example project http://www.filedropper.com/pointerupbug The element inside scrollrect throws OnPointerUp when you try to drag it. The element inside panel works as it should throwing OnPointerUp only when you release the button.

Show more comments

3 Replies

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

Answer by meat5000 · Oct 23, 2015 at 12:49 AM

Yep Im right. Without implementing the Drag function, it gets confused and fires the up.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class TestGridCell : MonoBehaviour, IPointerUpHandler, IPointerDownHandler, IDragHandler {
 
     public void OnPointerDown(PointerEventData eventData)
     {
         Debug.Log("OnPointerDown was called for object " + gameObject.name);
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         Debug.Log("OnPointerUp was called for object " + gameObject.name);
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         Debug.Log("Dragging " + gameObject.name);
     }
     
 }
Comment
Add comment · Show 10 · 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 TimGS · Oct 23, 2015 at 12:53 AM 0
Share

Thanks. That works.

I totally don't understand such logic, though and why it doesn't work in scrollrect only. =)

avatar image meat5000 ♦ TimGS · Oct 23, 2015 at 01:06 AM 0
Share

Yup, bizarre.

Only thing I can note is that not all the classes which list PointerEnter, down, up in the public functions (Scripting API) actually list OnDrag. ScrollRect does.

Glad to be of help.

avatar image nhut-diffgamestudio · Jul 15, 2016 at 06:24 PM 0
Share

It's exactly what i'm looking for. Thank you so much for your help!

avatar image Andrew-Le · Oct 02, 2016 at 06:01 PM 0
Share

same problem here, you dont save my day haha. But save my month x"D. upvote

avatar image Cairo · Nov 07, 2016 at 01:32 AM 0
Share

Great fix! I don't know if Unity has this on their bug list, but it would be good for them to fix.

avatar image mkanthan · Jan 06, 2017 at 09:54 PM 1
Share

I'm running into the same issue. However, I can't work around it this way because as soon as I add that OnDrag handler, my scrolling stops working if I try to scroll using this object. Did you come across this as well? OnDrag intercepts it.

avatar image Cairo mkanthan · Jan 08, 2017 at 07:31 PM 0
Share

@mkanthan, I would try to separate out your scroll view object from whatever it is that you are trying to do with OnDrag. If that is impossible for whatever reason, then maybe you could either try:

  1. passing on (redirecting) the eventData to your scroll view from the OnDrag

  2. update the scrolling yourself using the eventData in OnDrag

avatar image Bambivalent mkanthan · Mar 24, 2017 at 09:24 AM 3
Share

I had the same problems, did some research, and here is my final code which solved all issues. You have to put it on each button, but you don't need to link to the ScrollRect Parent. It also doesn't fire the Click() event if the user isn't OnEnter anymore:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 using System;
 
 public class $$anonymous$$enuButton: $$anonymous$$onoBehaviour, IPointerUpHandler, IPointerDownHandler,
                             IDragHandler, IBeginDragHandler, IEndDragHandler,
                             IPointerEnterHandler, IPointerExitHandler
 {
 
     private bool OnHover = false;
     private ScrollRect ScrollRectParent;
 
     public delegate void ClickHandler();
     public static event ClickHandler OnClick;
 
     public void Awake()
     {
         // find our ScrollRect parent
         if (GetComponentInParent<ScrollRect>() != null)
             ScrollRectParent = GetComponentInParent<ScrollRect>();
     }
 
 
     public void OnPointerUp(PointerEventData eventData)
     {
         if (OnHover && OnClick != null)
             OnClick();
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         OnHover = true;
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         OnHover = false;
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         if(ScrollRectParent != null)
             ScrollRectParent.OnDrag(eventData);
     }
     public void OnEndDrag(PointerEventData eventData)
     {
         if(ScrollRectParent != null)
             ScrollRectParent.OnEndDrag(eventData);
     }
     public void OnBeginDrag(PointerEventData eventData)
     {
         if(ScrollRectParent != null)
             ScrollRectParent.OnBeginDrag(eventData);
     }
 }

avatar image knguyen-mimbus Bambivalent · Jun 03, 2019 at 02:42 PM 0
Share

Bambivalent's solution is exactly whad I needed !

Show more comments
avatar image
0

Answer by OsoriPD · Apr 18, 2019 at 02:47 AM

thanks a lot! about this solution

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
avatar image
0

Answer by TheLastHylian · yesterday

This bug is still active.

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

47 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

Related Questions

onClick.AddListener() delegates don't appear on Inspector Events 1 Answer

Changing ScrollView content from the script 0 Answers

IDragHandler goes through buttons 0 Answers

Passing an int to a function from an onClick event gives the same value on every button. 0 Answers

How can I detect device orientation change in a fast and reliable way? 3 Answers


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