Make BaseSlider.SetHighValueWithoutNotify() public

Hello Unity

The BaseSlider class has lowValue and highValue properties that define the bounds of the slider. Unfortunately, setting these properties clamp the value of the slider which, in turn, can trigger a change event.

I think setting the bounds of a slider without triggering events is a legitimate use case. Just like there is a SetValueWithoutNotify() method there should also be a SetLowValueWithoutNotify() and SetHighValueWithoutNotify(). In fact the later exists already but is flagged as internal.

Would you please consider making SetHighValueWithoutNotify() public and adding SetLowValueWithoutNotify()? For now I have to use workarounds to set slider bounds without events and it makes the code uselessly convoluted.

Thank you very much.

Hi,
Ill add a task for us to look into this.
Do you have an example of where you would want to set the high value without sending an event?

Hello karl_jones. Thank you for considering my request.

Do you need the code or just the use case?

Long story short, I needed to code a scroll view that can be scrolled and panned infinitely in all directions. The size of the content container is recomputed when the user pans the view, move the content or resize the viewport.


As you can see there is a vertical and an horizontal scroller. I need to update their high values whenever the content size changes. I leave the low value at 0 but I set the high value to the content size. Internally scrollers use sliders.

Because sliders clamp their value when highValue is set that sometimes result in a change event being send which then cause the view to scroll at an invalid position. What I need in this case is a way to set both the value and the high value without triggering an event.

This is the workaround I came up with. I first set the value to 0, then set the high value knowing that clamping won’t change the value, and then set the value.

// Workaround to set scroller high value without triggering event
scroller.slider.SetValueWithoutNotify(0f);
scroller.highValue = scrollingRange;

// Set scroller position
scroller.slider.SetValueWithoutNotify(scrollPosition);

Another solution I tried was to always leave the high value to 1 and set the scroller value as a ratio. But for some reasons I don’t understand that caused the vertical scroller (not the horizontal one) to have only two possible positions. So I dropped that idea.

I hope this is clear enough.

1 Like

is reflection a no go for this? it’s for edit-mode, right?

Bcos this is an editor tool so you’re can get it via reflection :

var slider = new Slider();
var obj = slider as BaseSlider<float>;
var pinfo = obj.GetType().GetMethod("SetHighValueWithoutNotify", BindingFlags.Instance | BindingFlags.NonPublic);
pinfo.Invoke(obj, new System.Object[]{12f});
Debug.Log(slider.highValue);

Cache the method info, you should be good.
Don’t use this on runtime if you can, it’s totally fine on edit-mode

Thanks ill add the example to our task.
Reflection should be safe to use for now.