Extend the GUIColorAttribute functionality (#103)

This commit is contained in:
HoSHIZA 2023-05-01 15:00:53 +03:00 committed by GitHub
parent 697f32c616
commit 9d0f5faeab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 15 deletions

View File

@ -1,5 +1,6 @@
using TriInspector; using TriInspector;
using TriInspector.Drawers; using TriInspector.Drawers;
using TriInspector.Resolvers;
using UnityEngine; using UnityEngine;
[assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)] [assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)]
@ -11,13 +12,24 @@ namespace TriInspector.Drawers
public override void OnGUI(Rect position, TriProperty property, TriElement next) public override void OnGUI(Rect position, TriProperty property, TriElement next)
{ {
var oldColor = GUI.color; 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.color = newColor;
GUI.contentColor = newColor; GUI.contentColor = newColor;
next.OnGUI(position); next.OnGUI(position);
GUI.color = oldColor; GUI.color = oldColor;
GUI.contentColor = oldColor; GUI.contentColor = oldColor;
} }

View File

@ -6,15 +6,46 @@ public class Styling_GUIColorSample : ScriptableObject
[GUIColor(0.8f, 1.0f, 0.6f)] [GUIColor(0.8f, 1.0f, 0.6f)]
public Vector3 vec; public Vector3 vec;
[GUIColor(0.6f, 0.9f, 1.0f)] [GUIColor("0000FF")]
[Button] [Button]
public void BlueButton() 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] [Button]
public void RedButton() 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;
}
}
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using UnityEngine;
namespace TriInspector namespace TriInspector
{ {
@ -8,17 +9,40 @@ namespace TriInspector
[Conditional("UNITY_EDITOR")] [Conditional("UNITY_EDITOR")]
public class GUIColorAttribute : Attribute public class GUIColorAttribute : Attribute
{ {
public float R { get; } public Color Color { get; }
public float G { get; } public string GetColor { get; }
public float B { get; }
public float A { get; }
public GUIColorAttribute(float r, float g, float b, float a = 1f) public GUIColorAttribute(float r, float g, float b, float a = 1f)
{ {
R = r; Color = new Color(r, g, b, a);
G = g; }
B = b;
A = 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;
} }
} }
} }