- Home /
uGUI - How to listen for Slider-Handle release?
Hey all,
I have a volume slider and want to play a sound after the slider handle has been released (OnEndDrag). As suggested here, I'm using the IEndDragHandler interface. However the OnEndDrag and OnBeginDrag methods never get called. What am I doing wrong?
I'm using it like so:
public class UISimpleSlider: IEndDragHandler, IBeginDragHandler
{
[SerializeField] private Slider slider;
//... some slider init and refresh code goes here
private bool isBeingDragged = false;
public bool IsBeingDragged()
{
return isBeingDragged;
}
public void OnEndDrag(PointerEventData data)
{
Debug.Log("drag end");
isBeingDragged = false;
}
public void OnBeginDrag(PointerEventData data)
{
Debug.Log("drag start");
isBeingDragged = true;
}
}
At first I thought about working with onValueChanged, however this would fire the sound every time the value changes and not only after the handle has been realeased.
Answer by phineliner · Jan 02, 2016 at 12:21 AM
Solved it by using the inspector. You can add the event trigger component to your slider gameobject and set the BeginDrag and EndDrag events to call your specific methods. See screenshot below:
Yeah... But once we add event trigger the slider is no longer responsive....... What's the reason and how to overcome it... Someone answer....
The reason it's no longer responsive is probably because the EventTrigger is preventing all the event bubbling on the component. Ins$$anonymous$$d, make a $$anonymous$$onoBehaviour which implements the IBeginDragHandler and IEndDragHandler interfaces and handle the OnBeginDrag and OnEndDrag methods in that component. This component must be attached to the same GameObject that the Slider is on.
Can you explain what you mean by "component"? Another Game Object, or a script?
Your answer
Follow this Question
Related Questions
Detecting when the user starts and stop dragging a slider 1 Answer
Drag and jump mechanics? 2 Answers
Input.GetMouseButtonUp(0) not detected after drag 1 Answer
Pulling multiple objects 0 Answers
Drag and slide object by touch 1 Answer