// Draw the property inside the given rect public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // First get the attribute since it contains the range for the slider if (attribute is DefaultRangeAttribute) { DefaultRangeAttribute range = (DefaultRangeAttribute)attribute; // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer. if (property.propertyType == SerializedPropertyType.Float) { DisplayCheckboxAndControl(property, range, position, SetToDefaultFloat, DisplayFloatSlider, ref isEnabled, ref sliderValue); } else if (property.propertyType == SerializedPropertyType.Integer) { DisplayCheckboxAndControl(property, range, position, SetToDefaultInt, DisplayIntSlider, ref isEnabled, ref sliderValue); } else { EditorGUI.LabelField(position, label.text, "Use DefaultRange with float or int."); } } }
static void SetToDefaultInt(SerializedProperty property, DefaultRangeAttribute range) { property.floatValue = Mathf.RoundToInt(range.defaultNumber); }
static void SetToDefaultFloat(SerializedProperty property, DefaultRangeAttribute range) { property.floatValue = range.defaultNumber; }
static void DisplayIntSlider(SerializedProperty property, DefaultRangeAttribute range, Rect position, ref float value) { value = EditorGUI.IntSlider(position, Mathf.RoundToInt(value), Mathf.RoundToInt(range.min), Mathf.RoundToInt(range.max)); }
static void DisplayFloatSlider(SerializedProperty property, DefaultRangeAttribute range, Rect position, ref float value) { value = EditorGUI.Slider(position, value, range.min, range.max); }