UI Toolbox logoUI Toolbox
Support

Slider snap values

SliderSnap.SetValues makes a Slider or a SliderInt take only the values of a list, such as the frame rate caps 30, 60, 120, 144 and unlimited. A drag, the input field and slider.value = x jump to the nearest listed value, and a step with the d-pad, the arrow keys or the left stick moves to the next or the previous listed value.

An FPS cap slider on 120An FPS cap slider on 120

var fps = root.Q<SliderInt>("fpsCap");
SliderSnap.SetValues(fps, new[] { 30, 60, 120, 144, 300 });
fps.RegisterValueChangedCallback(evt =>
    label.text = evt.newValue >= 300
        ? "Unlimited" : evt.newValue.ToString());
Member Description
SetValues(Slider, float[]) Sets the values of a float slider. Null or an empty list takes the snapping away.
SetValues(SliderInt, int[]) The same for an int slider.
GetValues(VisualElement) The listed values in order, or null. The array is a copy.
Nearest, Next, Previous Static helpers on an ascending float[].

Rules

A slider shows its value on a line from lowValue to highValue, so the values 30, 60, 120, 144 and 300 sit at uneven distances. To show a last value as "Unlimited", give it a number at the end of the range and write the word in the value label, as the FormDemo scene does for its FPS cap.