static float LimitValue(DefaultNumberAttribute range, float value) { if ((range.NumberRange == DefaultNumberAttribute.Range.GreaterThanOrEqualTo) && (value < range.StartNumber)) { value = range.StartNumber; } else if ((range.NumberRange == DefaultNumberAttribute.Range.LessThanOrEqualTo) && (value > range.StartNumber)) { value = range.StartNumber; } return(value); }
// 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 DefaultNumberAttribute) { DefaultNumberAttribute range = (DefaultNumberAttribute)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, DisplayFloatField, ref isEnabled, ref sliderValue); } else if (property.propertyType == SerializedPropertyType.Integer) { DisplayCheckboxAndControl(property, range, position, SetToDefaultInt, DisplayIntField, ref isEnabled, ref sliderValue); } else { EditorGUI.LabelField(position, label.text, "Use DefaultNumber with float or int."); } } }
static void SetToDefaultInt(SerializedProperty property, DefaultNumberAttribute range) { property.floatValue = Mathf.RoundToInt(range.DefaultNumber); }
static void SetToDefaultFloat(SerializedProperty property, DefaultNumberAttribute range) { property.floatValue = range.DefaultNumber; }
static void DisplayIntField(SerializedProperty property, DefaultNumberAttribute range, Rect position, ref float value) { value = LimitValue(range, EditorGUI.IntField(position, Mathf.RoundToInt(value))); }
static void DisplayFloatField(SerializedProperty property, DefaultNumberAttribute range, Rect position, ref float value) { value = LimitValue(range, EditorGUI.FloatField(position, value)); }