I’ve got UI’Slider, that constantly slides to Slider.value = 0, if not dragged.
So, you drag slider to the end and if you release mouse at some point - slider will run to the initial position.
If you hold slider at value = 1 for a few seconds - something happend, and slider should be released automatically and go to the initial point.
To check if dragged I use EventTrigger with BeginDrag and EndDrag;
If not dragged = lerp slider value to the zero;
If dragged and value is 1 = start to count;
If counter filled = disable drag + some event;
And now I’m stumbled into this problem - how to disable drag?
I want slider to act like PointerClick or PointerUp is raised, even if user still holds mouse button.
(and user must click again to drag the slider).
I tried to set slider.interactable = false for one frame, or set EventSystem.current.SetSelectedGameObject(null);. No luck.
How to reset EventSystem.pointerDrag, or any other tricks to resolve this?
The only solution I had found is this:
var input = EventSystem.current.GetComponent<StandaloneInputModule>();
This code is called when I need to release my slider while it’s dragging by mouse.
AFAIK this BaseInputModule.DeactivateModule() forces main input module to reinitialize its values, like current dragged object. I tested this out, this call seemed to be safe to use
Perhaps someone else will find their way here.
Another option is to add an “EventTrigger” component to the slider in the inspector.
Add a new event of type “PointerUp”, and then you can assign functions to it.
If you simply want to reset the slider itself to 0 or another specific value on release, you just drag the slider into the empty Object and select Slider > float value, and then enter the value.
If you need something more complex, you can instead drag a GameObject that has your script on it and assign the function to YourScript > YourFunction.
Perhaps you want YourFunction to do more than just reset the slider, and you don’t want to add tons of functions to the Pointer Up event.
Just be careful when assigning references through the inspector like this.
It can become very messy if you link several different objects through inspector references.