Example #1
0
        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);
        }
Example #2
0
        // 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.");
                }
            }
        }
Example #3
0
 static void SetToDefaultInt(SerializedProperty property, DefaultNumberAttribute range)
 {
     property.floatValue = Mathf.RoundToInt(range.DefaultNumber);
 }
Example #4
0
 static void SetToDefaultFloat(SerializedProperty property, DefaultNumberAttribute range)
 {
     property.floatValue = range.DefaultNumber;
 }
Example #5
0
 static void DisplayIntField(SerializedProperty property, DefaultNumberAttribute range, Rect position, ref float value)
 {
     value = LimitValue(range, EditorGUI.IntField(position, Mathf.RoundToInt(value)));
 }
Example #6
0
 static void DisplayFloatField(SerializedProperty property, DefaultNumberAttribute range, Rect position, ref float value)
 {
     value = LimitValue(range, EditorGUI.FloatField(position, value));
 }