how do I add a listener on Unity GUI Slider to listen to the mouse down event on the slider handle?
Thank you in advance!
how do I add a listener on Unity GUI Slider to listen to the mouse down event on the slider handle?
Thank you in advance!
Unity UI Slider already listens to OnPointerDown as you can see it in the docs.
If you want to change the way it behaves or add some custom behaviour to this method you can do it different ways:
1- Add an EventTrigger to your object and selecting the OnPointerDown event as follow:
2- Declaring a new class that inherits from Slider class and overriding the OnPointerDown()
method like this:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CustomSlider: Slider
{
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
// Your code here
}
}
Note the second method will detect pointer down event on all the slider graphics while you can attach the EventTrigger component to the handle alone to detect pointer down event on this graphic element only.
Hope this helps,