IntRangeDrawer.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace DunGen.Editor.Drawers
  4. {
  5. [CustomPropertyDrawer(typeof(IntRange))]
  6. sealed class IntRangeDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. // Find properties
  11. var minProperty = property.FindPropertyRelative("Min");
  12. var maxProperty = property.FindPropertyRelative("Max");
  13. EditorGUI.BeginProperty(position, label, property);
  14. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  15. // Calculate rectangles
  16. const int fieldWidth = 50;
  17. const int dividerWidth = 10;
  18. const int padding = 5;
  19. float xPos = position.x;
  20. var minRect = new Rect(xPos, position.y, fieldWidth, position.height);
  21. xPos += fieldWidth + padding;
  22. var dividerRect = new Rect(xPos, position.y, dividerWidth, position.height);
  23. xPos += dividerWidth + padding;
  24. var maxRect = new Rect(xPos, position.y, fieldWidth, position.height);
  25. EditorGUI.BeginChangeCheck();
  26. EditorGUI.PropertyField(minRect, minProperty, GUIContent.none);
  27. EditorGUI.LabelField(dividerRect, "-");
  28. EditorGUI.BeginChangeCheck();
  29. EditorGUI.PropertyField(maxRect, maxProperty, GUIContent.none);
  30. if (EditorGUI.EndChangeCheck() && maxProperty.intValue < minProperty.intValue)
  31. maxProperty.intValue = minProperty.intValue;
  32. EditorGUI.EndProperty();
  33. }
  34. }
  35. }