Sharkfists

FancyRangePropertyDrawer.cs

Dec 7th, 2021
1,122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. [CustomPropertyDrawer(typeof(FancyIntRange))]
  5. [CustomPropertyDrawer(typeof(FancyUIntRange))]
  6. [CustomPropertyDrawer(typeof(FancyLongRange))]
  7. [CustomPropertyDrawer(typeof(FancyULongRange))]
  8. [CustomPropertyDrawer(typeof(FancyShortRange))]
  9. [CustomPropertyDrawer(typeof(FancyUShortRange))]
  10. [CustomPropertyDrawer(typeof(FancySByteRange))]
  11. [CustomPropertyDrawer(typeof(FancyByteRange))]
  12. [CustomPropertyDrawer(typeof(FancyFloatRange))]
  13. [CustomPropertyDrawer(typeof(FancyDoubleRange))]
  14. [CustomPropertyDrawer(typeof(FancyDecimalRange))]
  15. public class FancyRangePropertyDrawer : PropertyDrawer
  16. {
  17.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  18.     {
  19.         EditorGUI.BeginProperty(position, label, property);
  20.  
  21.         position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  22.  
  23.         int indent = EditorGUI.indentLevel;
  24.         EditorGUI.indentLevel = 0;
  25.  
  26.         const float labelWidth = 30;
  27.         const float spacing = 5;
  28.         float fieldWidth = (position.width - spacing * 3 - labelWidth * 2) / 2;
  29.        
  30.         Rect minLabel = new Rect(position.x,
  31.                                     position.y,
  32.                                     labelWidth,
  33.                                     position.height);
  34.         Rect minRect = new Rect(position.x + labelWidth + spacing,
  35.                                     position.y,
  36.                                     fieldWidth,
  37.                                     position.height);
  38.         Rect maxLabel = new Rect(position.x + labelWidth + spacing + fieldWidth + spacing,
  39.                                     position.y,
  40.                                     labelWidth,
  41.                                     position.height);
  42.         Rect maxRect = new Rect(position.x + labelWidth + spacing + fieldWidth + spacing + labelWidth + spacing,
  43.                                     position.y,
  44.                                     fieldWidth,
  45.                                     position.height);
  46.  
  47.         EditorGUI.LabelField(minLabel, "Min");
  48.         EditorGUI.PropertyField(minRect, property.FindPropertyRelative("Min"), GUIContent.none);
  49.         EditorGUI.LabelField(maxLabel, "Max");
  50.         EditorGUI.PropertyField(maxRect, property.FindPropertyRelative("Max"), GUIContent.none);
  51.  
  52.         EditorGUI.indentLevel = indent;
  53.         EditorGUI.EndProperty();
  54.     }
  55. }
  56.  
RAW Paste Data