- Home /
Cannot click on a button while Drag Events are called
Hi people.
I have created my own swiping menu for mobile and I am now facing the problem:
When I drag the screen, buttons "lose" their state (highlighted/pressed - to normal) even though a mouse is still over them, which means buttons cannot be pressed while dragging.
It seems like drag events cannot be handled together with button states at the same time. The only way to receive OnClick event is clicking on a button without moving the mouse (so that drag events are not called).
Is there a way to be able to "not lose" a button state while OnBeginDrag and OnDrag events are fired?
How would you solve that?
Thanks.
If I hold a mouse button down and then start moving the mouse - not just OnBeginDrag and OnDrag events are fired, but also OnPointerUp! (and mouse button is still held down). Why is this happening?
Is it a Unity bug?
Answer by xpavelos · Jun 17, 2017 at 10:31 AM
Okay I solved the issue.
The problem is that for some reason the OnPointerUp event is fired when drag events are in use. In order to avoid it - drag functions must be also implemented in a button (even if they are empty).
Here are more details: http://answers.unity3d.com/questions/1082179/mouse-drag-element-inside-scrollrect-throws-pointe.html
Answer by Addyarb · Jun 16, 2017 at 12:53 AM
Good question - it does seem like this should be an included functionality in the UI system - but it just doesn't seem to support both events used together. To remedy this, we'll create our own button component that will completely replace the existing button. (Note: you can still use the existing button component alongside this if you'd like).
We'll need to use Unity's Events and EventSystem libraries. To do this, just put these two new lines at the top of your script!
using UnityEngine.Events;
using UnityEngine.EventSystems;
Next we'll need to add two Interfaces. These will allow us to listen to Unity's event system (the same one that you're using when you add a Button or EventTrigger component). We want to listen to two of them in particular - called IPointerDownHandler and IPointerUpHandler.
You can add them by typing a comma after Monobehavior (or whatever other class you're inheriting from) and then typing every interface you need. Just start typing IPointer... and your auto-complete options will show you the rest. Check them out!
public class CustomButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
Now that we've added our interfaces, we can implement the methods that they require. In a nutshell, interfaces add specific functionality (like listening for events from an event system). However, you have to agree to implement the methods that they want you to - or it won't compile.
When you implement these methods, they have to share the same Access Modifiers, method name, and parameters.
The IPointerDownHandler interface wants us to use a public method called OnPointerDown. In that method, it wants us to have a PointerEventData parameter. So lets do that.
public void OnPointerDown(PointerEventData data)
{
OnButtonDown.Invoke();
}
And a similar case with IPointerUpHandler
public void OnPointerUp(PointerEventData data)
{
OnButtonUp.Invoke();
}
As you can see I'm just invoking our stored methods when these are called from Unity's event system.
And that's all there is to it! Full script below for yours and others' convenience.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class CustomButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
//Called when we click the button down
public UnityEvent OnButtonDown;
//Called when we click the button up
public UnityEvent OnButtonUp;
public void OnPointerDown(PointerEventData data)
{
OnButtonDown.Invoke();
}
public void OnPointerUp(PointerEventData data)
{
OnButtonUp.Invoke();
}
}
Thanks for broad answer and your effort - I appreciate that. Unfortunately, it doesn't solve the problem.
OnPointerUp event is fired when I start dragging (even when mouse is still down). It looks like it's Unity bug.
Your answer
Follow this Question
Related Questions
UI button mobile - want a button drag to register as a click 0 Answers
Highlight images in buttons children 0 Answers
Buttons not working properly. 0 Answers
UI button in Panel not clickable 2 Answers
Problem with button On Click 1 Answer