How to create an undo-able slider in a custom Editor that allows range modification?

( This is related to How to set the Range of a SerializedProperty at runtime? - Unity Answers 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

1 Answer

1

Create a custom property drawer:

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.

Thanks karlji1, this was indeed the way to do it :)