コード例 #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            label.tooltip = Label.GetTooltip(fieldInfo);

            var slider = attribute as SliderAttribute;

            if (property.propertyType == SerializedPropertyType.Float)
            {
                var value = EditorGUI.Slider(position, label, property.floatValue, slider.MinimumValue, slider.MaximumValue);
                value = SnapDrawer.Snap(value, slider.SnapValue);
                property.floatValue = Mathf.Clamp(value, slider.MinimumValue, slider.MaximumValue);
            }
            else if (property.propertyType == SerializedPropertyType.Integer)
            {
                var value = EditorGUI.IntSlider(position, label, property.intValue, Mathf.RoundToInt(slider.MinimumValue), Mathf.RoundToInt(slider.MaximumValue));
                value             = Mathf.RoundToInt(SnapDrawer.Snap(value, slider.SnapValue));
                property.intValue = Mathf.Clamp(value, Mathf.RoundToInt(slider.MinimumValue), Mathf.RoundToInt(slider.MaximumValue));
            }
            else
            {
                Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath);
                EditorGUI.PropertyField(position, property, label);
            }
        }
コード例 #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            label.tooltip = Label.GetTooltip(fieldInfo);

            // Use GetEndProperty instead of NextVisible since the max property should have a HideInInspector attribute
            // which would make NextVisible skip it.

            var minProperty = property;
            var maxProperty = property.GetEndProperty(true);
            var slider      = attribute as MinMaxSliderAttribute;

            if (minProperty.propertyType != maxProperty.propertyType)
            {
                Debug.LogWarningFormat(_mismatchedTypeWarning, property.propertyPath);
                EditorGUI.PropertyField(position, property, label);
            }
            else if (minProperty.propertyType != SerializedPropertyType.Float && minProperty.propertyType != SerializedPropertyType.Integer)
            {
                Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath);
                EditorGUI.PropertyField(position, property, label);
            }
            else
            {
                var rect = position;
                if (label != null)
                {
                    rect = EditorGUI.PrefixLabel(position, label);
                }

                var minRect    = new Rect(rect.x, rect.y, rect.width * 0.2f, EditorGUIUtility.singleLineHeight);
                var sliderRect = new Rect(minRect.xMax + 5, rect.y, rect.width * 0.6f - 10, EditorGUIUtility.singleLineHeight);
                var maxRect    = new Rect(sliderRect.xMax + 5, rect.y, rect.width * 0.2f, EditorGUIUtility.singleLineHeight);

                if (minProperty.propertyType == SerializedPropertyType.Float)
                {
                    var minimum = EditorGUI.FloatField(minRect, minProperty.floatValue);
                    var maximum = EditorGUI.FloatField(maxRect, maxProperty.floatValue);

                    EditorGUI.MinMaxSlider(sliderRect, ref minimum, ref maximum, slider.MinimumValue, slider.MaximumValue);

                    minimum = SnapDrawer.Snap(minimum, slider.SnapValue);
                    maximum = SnapDrawer.Snap(maximum, slider.SnapValue);

                    minProperty.floatValue = Mathf.Clamp(minimum, slider.MinimumValue, maximum);
                    maxProperty.floatValue = Mathf.Clamp(maximum, minimum, slider.MaximumValue);
                }
                else if (minProperty.propertyType == SerializedPropertyType.Integer)
                {
                    var minimum = (float)EditorGUI.IntField(minRect, minProperty.intValue);
                    var maximum = (float)EditorGUI.IntField(maxRect, maxProperty.intValue);

                    EditorGUI.MinMaxSlider(sliderRect, ref minimum, ref maximum, slider.MinimumValue, slider.MaximumValue);

                    var min = Mathf.RoundToInt(SnapDrawer.Snap(minimum, slider.SnapValue));
                    var max = Mathf.RoundToInt(SnapDrawer.Snap(maximum, slider.SnapValue));

                    minProperty.intValue = Mathf.Clamp(min, Mathf.RoundToInt(slider.MinimumValue), max);
                    maxProperty.intValue = Mathf.Clamp(max, min, Mathf.RoundToInt(slider.MaximumValue));
                }
            }
        }