Skip to content

Commit 8e9e488

Browse files
committed
Unit attribute improvements
1 parent 03a2441 commit 8e9e488

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed
Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using TriInspector;
22
using TriInspector.Drawers;
3+
using TriInspector.Resolvers;
4+
using TriInspector.Utilities;
35
using UnityEditor;
46
using 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
}

Editor.Samples/Decorators/Decorators_UnitSample.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Globalization;
23
using TriInspector;
34
using UnityEngine;
45

@@ -7,6 +8,9 @@ public class Decorators_UnitSample : ScriptableObject
78
[Unit("My custom Unit")]
89
public float freeTextUnit;
910

11+
[Unit("$" + nameof(GetDynamicUnit))]
12+
public float dynamicUnit;
13+
1014
[Unit(UnitAttribute.Meter)]
1115
public float lengthInMeters;
1216

@@ -108,4 +112,9 @@ public class Decorators_UnitSample : ScriptableObject
108112

109113
[Unit(UnitAttribute.MeterPerSquareSecond)]
110114
public float accelerationInMetersPerSquareSecond;
115+
116+
private string GetDynamicUnit()
117+
{
118+
return DateTime.Now.ToString(CultureInfo.CurrentCulture);
119+
}
111120
}

Editor/Utilities/TriGuiHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ namespace TriInspector.Utilities
88
{
99
public static class TriGuiHelper
1010
{
11+
private static readonly GUIContent TempContentShared = new GUIContent();
1112
private static readonly Stack<Object> TargetObjects = new Stack<Object>();
1213

14+
internal static GUIContent TempContent(string text)
15+
{
16+
TempContentShared.text = text;
17+
return TempContentShared;
18+
}
19+
1320
internal static bool IsAnyEditorPushed()
1421
{
1522
return TargetObjects.Count > 0;

0 commit comments

Comments
 (0)