- Home /
UI slider "End slide" event
Hello,
The Input Field script form Unity UI has "On Value Changed" and "On End Edit" event listeners. Is there any event for Slider similar to the second one from Input Field?
I mean I would like to have an event that fires after user finishes dragging the handle on Slider, so that I will adjust everything to the final value the user wants.
I was thinking of putting some kind of coroutine that will track 0.3 seconds after last "OnValueChanged" event and then readjust everything, but that sounds very messy.
In case someone had this trouble or knows how to fix it - would be glad to see an answer =)
Answer by LK84 · Dec 30, 2016 at 08:30 AM
Drag this script on your Slider GameObject:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class SliderDrag : MonoBehaviour,IPointerUpHandler {
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Sliding finished");
}
}
Answer by gheeler · Feb 15, 2018 at 11:43 AM
I came across this and thought I'd add a bit so you have an event that you can access elsewhere like OnValueChanged
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public delegate void EndSliderDragEventHandler (float val);
[RequireComponent(typeof (Slider))]
public class SliderDrag : MonoBehaviour, IPointerUpHandler
{
public event EndSliderDragEventHandler EndDrag;
private float SliderValue {
get {
return gameObject.GetComponent<Slider>().value;
}
}
public void OnPointerUp (PointerEventData data)
{
if (EndDrag != null)
{
EndDrag(SliderValue);
}
}
}
Answer by geonak · Jun 10, 2020 at 09:48 AM
I came across this as I was looking for something similar, I thought I'd add my finding as it might help someone.
The easiest way I found is to add an Event trigger component to the slider, in addition to the slider component. You then add a new 'pointer up' event type to that Event trigger, and you set it the usual way to call whatever function you want.
This way the standard slider component will handle the On Value Changed, and your new Event trigger will handle the on release.