Skip to content

Commit 9d0f5fa

Browse files
authored
Extend the GUIColorAttribute functionality (#103)
1 parent 697f32c commit 9d0f5fa

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

Editor.Extras/Drawers/GUIColorDrawer.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using TriInspector;
22
using TriInspector.Drawers;
3+
using TriInspector.Resolvers;
34
using UnityEngine;
45

56
[assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)]
@@ -11,13 +12,24 @@ public class GUIColorDrawer : TriAttributeDrawer<GUIColorAttribute>
1112
public override void OnGUI(Rect position, TriProperty property, TriElement next)
1213
{
1314
var oldColor = GUI.color;
14-
var newColor = new Color(Attribute.R, Attribute.G, Attribute.B, Attribute.A);
15-
15+
var newColor = Color.white;
16+
17+
if (string.IsNullOrEmpty(Attribute.GetColor))
18+
{
19+
newColor = Attribute.Color;
20+
}
21+
else
22+
{
23+
var colorResolver = ValueResolver.Resolve<Color>(property.Definition, Attribute.GetColor ?? "");
24+
25+
newColor = colorResolver.GetValue(property, Color.white);
26+
}
27+
1628
GUI.color = newColor;
1729
GUI.contentColor = newColor;
18-
30+
1931
next.OnGUI(position);
20-
32+
2133
GUI.color = oldColor;
2234
GUI.contentColor = oldColor;
2335
}

Editor.Samples/Styling/Styling_GUIColorSample.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,46 @@ public class Styling_GUIColorSample : ScriptableObject
66
[GUIColor(0.8f, 1.0f, 0.6f)]
77
public Vector3 vec;
88

9-
[GUIColor(0.6f, 0.9f, 1.0f)]
9+
[GUIColor("0000FF")]
1010
[Button]
1111
public void BlueButton()
1212
{
1313
}
1414

15-
[GUIColor(1.0f, 0.6f, 0.6f)]
15+
[GUIColor("cyan")]
16+
[Button]
17+
public void CyanButton()
18+
{
19+
}
20+
21+
[GUIColor("$GetGreenColor")]
22+
[Button]
23+
public void GreenButton()
24+
{
25+
}
26+
27+
[GUIColor(255, 75, 75)]
1628
[Button]
1729
public void RedButton()
1830
{
1931
}
32+
33+
[GUIColor("$GetColor")]
34+
[Button(ButtonSizes.Large)]
35+
public void ColoredButton()
36+
{
37+
}
38+
39+
private Color GetGreenColor => Color.green;
40+
41+
private Color GetColor
42+
{
43+
get
44+
{
45+
var time = (float) UnityEditor.EditorApplication.timeSinceStartup;
46+
var hue = time * 0.225f % 1f;
47+
var color = Color.HSVToRGB(hue, 1f, 1f);
48+
return color;
49+
}
50+
}
2051
}
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using UnityEngine;
34

45
namespace TriInspector
56
{
@@ -8,17 +9,40 @@ namespace TriInspector
89
[Conditional("UNITY_EDITOR")]
910
public class GUIColorAttribute : Attribute
1011
{
11-
public float R { get; }
12-
public float G { get; }
13-
public float B { get; }
14-
public float A { get; }
15-
12+
public Color Color { get; }
13+
public string GetColor { get; }
14+
1615
public GUIColorAttribute(float r, float g, float b, float a = 1f)
1716
{
18-
R = r;
19-
G = g;
20-
B = b;
21-
A = a;
17+
Color = new Color(r, g, b, a);
18+
}
19+
20+
public GUIColorAttribute(byte r, byte g, byte b, byte a = byte.MaxValue)
21+
{
22+
Color = new Color32(r, g, b, a);
23+
}
24+
25+
public GUIColorAttribute(string value)
26+
{
27+
if (value.StartsWith("$"))
28+
{
29+
GetColor = value;
30+
31+
return;
32+
}
33+
34+
if (ColorUtility.TryParseHtmlString(value, out var color))
35+
{
36+
}
37+
else if (ColorUtility.TryParseHtmlString($"#{value}", out color))
38+
{
39+
}
40+
else
41+
{
42+
color = Color.white;
43+
}
44+
45+
Color = color;
2246
}
2347
}
2448
}

0 commit comments

Comments
 (0)