- Home /
How to create an undo-able slider in a custom Editor that allows range modification?
( This is related to http://answers.unity3d.com/questions/585584/how-to-set-the-range-of-a-serializedproperty-at-ru.html#comment-586007 but after getting some answers I realized I needed to ask the question in a more open-ended way.)
I'm creating an Editor class for a custom inspector. The purpose is to let the user create some geometry in the scene, and then control its width with a slider. (…. and lots of other unrelated stuff.)
The project was finished when I discovered that it is mandatory for Editors to implement Undo functionality. ( http://answers.unity3d.com/questions/583052/undo-mandatory-for-editor-tools.html )
I learned about SerializedProperty, and how they will automagically encapsulate 'undo' behavior, re-wrote the editor, and again everything works, except…
The 'width' slider needs to be restricted to certain values depending on other user-set variables.
I know how to impose a fixed range:
[Range(1, 10)]
public int width = 2;
But this won't work, because at some point in use, the Range will need to be different.
I was shown today how to restrict a general variable to certain values using properties, but in this case it has to be (I think!) a SerializedProperty (to support Undo), and has to be correctly represented by the 'width' slider in the inspector:
EditorGUILayout.PropertyField(width);
Rather than use terms that I haven't mastered yet, my more general question is...
Is it possible to have a slider in a custom editor that:
• correctly reacts to Undo, and...
• can have its min and max values modified during use?
Thanks
Answer by karljj1 · Nov 29, 2013 at 10:07 AM
Create a custom property drawer:
http://docs.unity3d.com/Documentation/ScriptReference/PropertyDrawer.html
The Range attribute just causes a Range property drawer to be used. You can make your own attribute and link it to a custom property drawer that allows for a changing range value.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123041im_/https://answers.unity.com/themes/thub/images/avi.jpg)