1
+ using UnityEditor ;
2
+ using UnityEngine ;
3
+
4
+ namespace Lachee . Attributes . Editor
5
+ {
6
+ [ CustomPropertyDrawer ( typeof ( SlideAttribute ) ) ]
7
+ public class SlideDrawer : PropertyDrawer
8
+ {
9
+ const float miniLabelVerticalOffset = - 7 ;
10
+
11
+ static GUIStyle miniLabelStyle ;
12
+ static SlideDrawer ( )
13
+ {
14
+ miniLabelStyle = new GUIStyle ( EditorStyles . miniLabel ) ;
15
+ }
16
+
17
+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
18
+ {
19
+ SlideAttribute attr = ( SlideAttribute ) attribute ;
20
+
21
+
22
+ // Draw the left and right
23
+ if ( attr . LeftLabel != null )
24
+ {
25
+ miniLabelStyle . alignment = TextAnchor . UpperLeft ;
26
+ EditorGUI . LabelField ( new Rect ( position . x + EditorGUIUtility . labelWidth , position . y + miniLabelVerticalOffset , position . width , position . height ) , attr . LeftLabel , miniLabelStyle ) ;
27
+ }
28
+
29
+ //style.alignment = TextAnchor.LowerRight;
30
+ if ( attr . RightLabel != null )
31
+ {
32
+ float padding = property . propertyType == SerializedPropertyType . Float || property . propertyType == SerializedPropertyType . Integer ? - EditorGUIUtility . fieldWidth - 5 : 0 ;
33
+ miniLabelStyle . alignment = TextAnchor . UpperRight ;
34
+ EditorGUI . LabelField ( new Rect ( position . x + EditorGUIUtility . labelWidth , position . y + miniLabelVerticalOffset , position . width - EditorGUIUtility . labelWidth + padding , position . height ) , attr . RightLabel , miniLabelStyle ) ;
35
+ }
36
+
37
+ Rect sliderBox = new Rect ( position . x , position . y , position . width , EditorGUIUtility . singleLineHeight ) ;
38
+
39
+ // Draw the slider
40
+ switch ( property . propertyType )
41
+ {
42
+ case SerializedPropertyType . Float :
43
+ EditorGUI . Slider ( sliderBox , property , attr . Min , attr . Max , label ) ;
44
+ break ;
45
+ case SerializedPropertyType . Integer :
46
+ EditorGUI . IntSlider ( sliderBox , property , ( int ) attr . Min , ( int ) attr . Max , label ) ;
47
+ break ;
48
+ case SerializedPropertyType . Vector2 :
49
+ case SerializedPropertyType . Vector2Int :
50
+ MinMaxSlider ( sliderBox , property , attr . Min , attr . Max , label ) ;
51
+ break ;
52
+ default :
53
+ EditorGUI . LabelField ( position , label , new GUIContent ( "Slider must be of type float, int, Vector2, or Vector2Int" ) ) ;
54
+ return ;
55
+ }
56
+
57
+ }
58
+
59
+ private static void MinMaxSlider ( Rect position , SerializedProperty property , float min , float max , GUIContent label )
60
+ {
61
+ float minValue = 0 ;
62
+ float maxValue = 0 ;
63
+
64
+ if ( property . propertyType == SerializedPropertyType . Vector2 )
65
+ {
66
+ minValue = property . vector2Value . x ;
67
+ maxValue = property . vector2Value . y ;
68
+ EditorGUI . MinMaxSlider ( position , label , ref minValue , ref maxValue , min , max ) ;
69
+ property . vector2Value = new Vector2 ( minValue , maxValue ) ;
70
+ }
71
+ else if ( property . propertyType == SerializedPropertyType . Vector2Int )
72
+ {
73
+ minValue = property . vector2IntValue . x ;
74
+ maxValue = property . vector2IntValue . y ;
75
+ EditorGUI . MinMaxSlider ( position , label , ref minValue , ref maxValue , min , max ) ;
76
+ property . vector2IntValue = new Vector2Int ( Mathf . RoundToInt ( minValue ) , Mathf . RoundToInt ( maxValue ) ) ;
77
+ }
78
+ else
79
+ {
80
+ throw new System . ArgumentException ( "The property must be a vector2 type" ) ;
81
+ }
82
+ }
83
+
84
+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
85
+ {
86
+ float height = base . GetPropertyHeight ( property , label ) ;
87
+
88
+ SlideAttribute attr = attribute as SlideAttribute ;
89
+ if ( attr != null && ( attr . LeftLabel != null || attr . RightLabel != null ) )
90
+ height += - miniLabelVerticalOffset ;
91
+
92
+ return height ;
93
+ }
94
+ }
95
+ }
0 commit comments