Skip to content

Commit a292fec

Browse files
committed
Optimize GUIColorDrawer
1 parent 9d0f5fa commit a292fec

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

Editor.Extras/Drawers/GUIColorDrawer.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using TriInspector;
1+
using JetBrains.Annotations;
2+
using TriInspector;
23
using TriInspector.Drawers;
34
using TriInspector.Resolvers;
45
using UnityEngine;
@@ -9,27 +10,33 @@ namespace TriInspector.Drawers
910
{
1011
public class GUIColorDrawer : TriAttributeDrawer<GUIColorAttribute>
1112
{
12-
public override void OnGUI(Rect position, TriProperty property, TriElement next)
13+
[CanBeNull] private ValueResolver<Color> _colorResolver;
14+
15+
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
1316
{
14-
var oldColor = GUI.color;
15-
var newColor = Color.white;
16-
17-
if (string.IsNullOrEmpty(Attribute.GetColor))
17+
if (!string.IsNullOrEmpty(Attribute.GetColor))
1818
{
19-
newColor = Attribute.Color;
19+
_colorResolver = ValueResolver.Resolve<Color>(propertyDefinition, Attribute.GetColor);
2020
}
21-
else
21+
22+
if (_colorResolver != null && _colorResolver.TryGetErrorString(out var error))
2223
{
23-
var colorResolver = ValueResolver.Resolve<Color>(property.Definition, Attribute.GetColor ?? "");
24-
25-
newColor = colorResolver.GetValue(property, Color.white);
24+
return error;
2625
}
27-
26+
27+
return TriExtensionInitializationResult.Ok;
28+
}
29+
30+
public override void OnGUI(Rect position, TriProperty property, TriElement next)
31+
{
32+
var oldColor = GUI.color;
33+
var newColor = _colorResolver?.GetValue(property, Color.white) ?? Attribute.Color;
34+
2835
GUI.color = newColor;
2936
GUI.contentColor = newColor;
30-
37+
3138
next.OnGUI(position);
32-
39+
3340
GUI.color = oldColor;
3441
GUI.contentColor = oldColor;
3542
}

0 commit comments

Comments
 (0)