- Home /
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
Care to post an example project?
Did you implement IDragHandler?
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.
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);
}
}
Thanks. That works.
I totally don't understand such logic, though and why it doesn't work in scrollrect only. =)
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.
It's exactly what i'm looking for. Thank you so much for your help!
same problem here, you dont save my day haha. But save my month x"D. upvote
Great fix! I don't know if Unity has this on their bug list, but it would be good for them to fix.
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.
@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:
passing on (redirecting) the eventData to your scroll view from the OnDrag
update the scrolling yourself using the eventData in OnDrag
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);
}
}
Bambivalent's solution is exactly whad I needed !