Needed: A UI Button Sensitive to MouseUp, Drag etc

Hi,
I want to create a touch sensiteive UI Button. What I mean is, I would like to detect tap, drag, up… etc.

Tho the scripting pages list OnMouseDrag() etc under the UI Button, if I place…

public OnMouseDrag(){
print("dragging");
}

…nothing occurs.

I added

using UnityEngine.UI;

… still no luck.

I googled about, this chap created a wrapper…

public class UIInput : MonoBehaviour, IPointerDownHandler
{
public List<Action<PointerEventData>> OnMouseDownListeners=new List<Action<PointerEventData>>();
public void OnPointerDown(PointerEventData eventData)
{
foreach(var callback inOnMouseDownListeners)
{
callback(eventData);
}
}
public void AddOnMouseDownListener(Action<PointerEventData> action ){
OnMouseDownListeners.Add(action);
}
}

…but i don’t understand it, and I am traditionally hesitant to add code I do not get.

So, is this wrapper above the way to go? What is the best way to achieve a dynamic, sensitive button? I realize I could just monitor in Update, but I was hoping for a more dynamic system.

Unity is handling the events via Interfaces.
What do you want your button do do exactly / what should he react to?
The IPointerDownHandler interface you named in your example will send pointer down events to the OnPointerDown function.
In the OnPointerDown function you can do what ever you like to do if the pointer is pressed down.

In the documentation you will find the other interfaces for the different types of events.
http://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.OnPointerDown.html

1 Like