11using TriInspector ;
22using TriInspector . Drawers ;
3+ using TriInspector . Resolvers ;
4+ using TriInspector . Utilities ;
35using UnityEditor ;
46using UnityEngine ;
57
@@ -12,34 +14,50 @@ public class UnitDrawer : TriAttributeDrawer<UnitAttribute>
1214 /// <summary>
1315 /// Defines the padding to the right of the unit label towards the editable input field
1416 /// </summary>
15- private const int paddingRight = 5 ;
16- private GUIStyle unitStyle ;
17- private GUIContent content ;
17+ private const int PaddingRight = 5 ;
1818
19- public override void OnGUI ( Rect position , TriProperty property , TriElement next )
19+ private ValueResolver < string > _unitResolver ;
20+
21+ public override TriExtensionInitializationResult Initialize ( TriPropertyDefinition propertyDefinition )
2022 {
21- if ( unitStyle == null ) {
22- unitStyle = new ( EditorStyles . label ) ;
23- }
23+ base . Initialize ( propertyDefinition ) ;
24+
25+ _unitResolver = ValueResolver . ResolveString ( propertyDefinition , Attribute . unitToDisplay ) ;
2426
25- if ( content == null ) {
26- content = new ( Attribute . unitToDisplay ) ;
27+ if ( _unitResolver . TryGetErrorString ( out var error ) )
28+ {
29+ return error ;
2730 }
2831
29- Vector2 size = unitStyle . CalcSize ( content ) ;
32+ return TriExtensionInitializationResult . Ok ;
33+ }
34+
35+ public override void OnGUI ( Rect position , TriProperty property , TriElement next )
36+ {
37+ var unit = _unitResolver . GetValue ( property , "" ) ;
38+ var size = Styles . UnitStyle . CalcSize ( TriGuiHelper . TempContent ( unit ) ) ;
3039
31- var unitRect = new Rect ( position . xMax - size . x - paddingRight , position . y , size . x , position . height ) ;
40+ var unitRect = new Rect ( position . xMax - size . x - PaddingRight , position . y , size . x , position . height ) ;
3241
3342 // Render the editable input field
3443 next . OnGUI ( position ) ;
3544
3645 //Change color to grey
37- var tmpColor = GUI . color ;
38- GUI . color = Color . grey ;
39- // Render the unit as a suffix in the unitRect
40- EditorGUI . LabelField ( unitRect , content ) ;
41- // Restore the original color
42- GUI . color = tmpColor ;
46+ using ( TriGuiHelper . PushColor ( Color . grey ) )
47+ {
48+ // Render the unit as a suffix in the unitRect
49+ EditorGUI . LabelField ( unitRect , unit ) ;
50+ }
51+ }
52+
53+ private static class Styles
54+ {
55+ public static readonly GUIStyle UnitStyle ;
56+
57+ static Styles ( )
58+ {
59+ UnitStyle = new GUIStyle ( EditorStyles . label ) ;
60+ }
4361 }
4462 }
4563}
0 commit comments