using UnityEngine; using UnityEditor; using Geri.MinMaxSlider; [CustomPropertyDrawer(typeof(MinMaxSliderAttribute))] public class MinMaxSliderDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){ var minMaxAttribute = (MinMaxSliderAttribute)attribute; var propertyType = property.propertyType; label.tooltip = minMaxAttribute.min.ToString("F2") + " to " + minMaxAttribute.max.ToString("F2"); if(propertyType == SerializedPropertyType.Vector2){ Vector2 vector = property.vector2Value; float minVal = vector.x; float maxVal = vector.y; int originalIndentLevel = EditorGUI.indentLevel; EditorGUI.BeginProperty(position, label, property); position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); EditorGUI.indentLevel = 0; float fieldWidth = position.width / 4f - 4f; float sliderWidth = position.width / 2f; position.width = fieldWidth; minVal = EditorGUI.FloatField(position, minVal); position.x += fieldWidth + 4f; position.width = sliderWidth; EditorGUI.MinMaxSlider(position, ref minVal, ref maxVal, minMaxAttribute.min, minMaxAttribute.max); position.x += sliderWidth + 4f; position.width = fieldWidth; maxVal = EditorGUI.FloatField(position, maxVal); if (maxVal < minMaxAttribute.min){ minVal = minMaxAttribute.min; }else if (maxVal > minMaxAttribute.max){ maxVal = minMaxAttribute.max; } vector = new Vector2(minVal > maxVal ? maxVal : minVal, maxVal); property.vector2Value = vector; EditorGUI.EndProperty(); EditorGUI.indentLevel = originalIndentLevel; }else if(propertyType == SerializedPropertyType.Vector2Int){ Vector2Int vector = property.vector2IntValue; float minVal = vector.x; float maxVal = vector.y; int originalIndentLevel = EditorGUI.indentLevel; EditorGUI.BeginProperty(position, label, property); position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); EditorGUI.indentLevel = 0; float fieldWidth = position.width / 4f - 4f; float sliderWidth = position.width / 2f; position.width = fieldWidth; minVal = EditorGUI.FloatField(position, minVal); position.x += fieldWidth + 4f; position.width = sliderWidth; EditorGUI.MinMaxSlider(position, ref minVal, ref maxVal, minMaxAttribute.min, minMaxAttribute.max); position.x += sliderWidth + 4f; position.width = fieldWidth; maxVal = EditorGUI.FloatField(position, maxVal); if (maxVal < minMaxAttribute.min){ minVal = minMaxAttribute.min; }else if (maxVal > minMaxAttribute.max){ maxVal = minMaxAttribute.max; } vector = new Vector2Int(Mathf.FloorToInt(minVal > maxVal ? maxVal : minVal), Mathf.FloorToInt(maxVal)); property.vector2IntValue = vector; EditorGUI.EndProperty(); EditorGUI.indentLevel = originalIndentLevel; } } }