Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Editor.Extras/Drawers/GUIColorDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Resolvers;
using UnityEngine;

[assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)]
Expand All @@ -11,13 +12,24 @@ public class GUIColorDrawer : TriAttributeDrawer<GUIColorAttribute>
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<Color>(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;
}
Expand Down
35 changes: 33 additions & 2 deletions Editor.Samples/Styling/Styling_GUIColorSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
42 changes: 33 additions & 9 deletions Runtime/Attributes/GUIColorAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using UnityEngine;

namespace TriInspector
{
Expand All @@ -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;
}
}
}