diff --git a/Editor.Extras/Drawers/GUIColorDrawer.cs b/Editor.Extras/Drawers/GUIColorDrawer.cs index 5f531ff..65c6d04 100644 --- a/Editor.Extras/Drawers/GUIColorDrawer.cs +++ b/Editor.Extras/Drawers/GUIColorDrawer.cs @@ -1,5 +1,6 @@ using TriInspector; using TriInspector.Drawers; +using TriInspector.Resolvers; using UnityEngine; [assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)] @@ -11,13 +12,24 @@ namespace TriInspector.Drawers public override void OnGUI(Rect position, TriProperty property, TriElement next) { var oldColor = GUI.color; - var newColor = new Color(Attribute.R, Attribute.G, Attribute.B, Attribute.A); - + var newColor = Color.white; + + if (string.IsNullOrEmpty(Attribute.GetColor)) + { + newColor = Attribute.Color; + } + else + { + var colorResolver = ValueResolver.Resolve(property.Definition, Attribute.GetColor ?? ""); + + newColor = colorResolver.GetValue(property, Color.white); + } + GUI.color = newColor; GUI.contentColor = newColor; - + next.OnGUI(position); - + GUI.color = oldColor; GUI.contentColor = oldColor; } diff --git a/Editor.Samples/Styling/Styling_GUIColorSample.cs b/Editor.Samples/Styling/Styling_GUIColorSample.cs index 4d42ac9..d308347 100644 --- a/Editor.Samples/Styling/Styling_GUIColorSample.cs +++ b/Editor.Samples/Styling/Styling_GUIColorSample.cs @@ -6,15 +6,46 @@ public class Styling_GUIColorSample : ScriptableObject [GUIColor(0.8f, 1.0f, 0.6f)] public Vector3 vec; - [GUIColor(0.6f, 0.9f, 1.0f)] + [GUIColor("0000FF")] [Button] public void BlueButton() { } - [GUIColor(1.0f, 0.6f, 0.6f)] + [GUIColor("cyan")] + [Button] + public void CyanButton() + { + } + + [GUIColor("$GetGreenColor")] + [Button] + public void GreenButton() + { + } + + [GUIColor(255, 75, 75)] [Button] public void RedButton() { } + + [GUIColor("$GetColor")] + [Button(ButtonSizes.Large)] + public void ColoredButton() + { + } + + private Color GetGreenColor => Color.green; + + private Color GetColor + { + get + { + var time = (float) UnityEditor.EditorApplication.timeSinceStartup; + var hue = time * 0.225f % 1f; + var color = Color.HSVToRGB(hue, 1f, 1f); + return color; + } + } } \ No newline at end of file diff --git a/Runtime/Attributes/GUIColorAttribute.cs b/Runtime/Attributes/GUIColorAttribute.cs index b6ed8f8..7771db3 100644 --- a/Runtime/Attributes/GUIColorAttribute.cs +++ b/Runtime/Attributes/GUIColorAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using UnityEngine; namespace TriInspector { @@ -8,17 +9,40 @@ namespace TriInspector [Conditional("UNITY_EDITOR")] public class GUIColorAttribute : Attribute { - public float R { get; } - public float G { get; } - public float B { get; } - public float A { get; } - + public Color Color { get; } + public string GetColor { get; } + public GUIColorAttribute(float r, float g, float b, float a = 1f) { - R = r; - G = g; - B = b; - A = a; + Color = new Color(r, g, b, a); + } + + public GUIColorAttribute(byte r, byte g, byte b, byte a = byte.MaxValue) + { + Color = new Color32(r, g, b, a); + } + + public GUIColorAttribute(string value) + { + if (value.StartsWith("$")) + { + GetColor = value; + + return; + } + + if (ColorUtility.TryParseHtmlString(value, out var color)) + { + } + else if (ColorUtility.TryParseHtmlString($"#{value}", out color)) + { + } + else + { + color = Color.white; + } + + Color = color; } } } \ No newline at end of file