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.Drawers;
using TriInspector.Resolvers;
using UnityEngine;
[assembly: RegisterTriAttributeDrawer(typeof(GUIColorDrawer), TriDrawerOrder.Decorator)]
@ -11,7 +12,18 @@ 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<Color>(property.Definition, Attribute.GetColor ?? "");
newColor = colorResolver.GetValue(property, Color.white);
}
GUI.color = newColor;
GUI.contentColor = newColor;

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}