- Home /
How to limit the amount of decimals with a slider in Unity Editor OnInspectorGUI?
So I have been fiddling around with the editor a lot and made a cool mostly horizontal script for level editting simple layer based levels. For this I have sliders that control the height of the layered walls which can vary between 2f and 3f and can be any value in between, except I would like to limit this to go in steps of 0.1f.
Currently I have:
EditorGUI.Slider(new Rect(posLeft, pos.y, sliderWidth, rowHeight), rightScale, 2.0F, 3.0F, GUIContent.none);
I tried looking and ways to go around this by seperating the value shown and the slider but haven't found anything that limits the stepsize of the slider. I know the ways to convert float values by either Rounding the value or using ToString("0.#") but as the slider decides what value is shown I can't manipulate it later in the editor script.
Answer by Adam-Mechtley · Nov 07, 2016 at 09:43 PM
If you round your float value before passing it to the Slider() method, and round the value you get back from the Slider() method, that should do the trick. You can save yourself that calculation of the rounding you get back by wrapping your call to Slider() in EditorGUI.BeginChangeCheck()/EndChangeCheck(). For example:
rightScale = Mathf.Round(rightScale * 10f) * 0.1f;
EditorGUI.BeginChangeCheck();
{
EditorGUI.Slider(new Rect(posLeft, pos.y, sliderWidth, rowHeight), rightScale, 2f, 3f, GUIContent.none);
}
if (EditorGUI.EndChangeCheck())
{
rightScale = Mathf.Round(rightScale * 10f) * 0.1f;
// apply rightScale back to your object or whatever
}
Thanks man that does the trick, I didn't know about BeginChangeCheck() and EndChangeCheck().