Skip to content

Instantly share code, notes, and snippets.

@FNGgames
Last active June 5, 2023 11:20
Show Gist options
  • Save FNGgames/550a55a5ddb06248261493cbd11729cc to your computer and use it in GitHub Desktop.
Save FNGgames/550a55a5ddb06248261493cbd11729cc to your computer and use it in GitHub Desktop.
Odin Color Box and Color Foldout Groups
#if UNITY_2020_2_OR_NEWER && ODIN_INSPECTOR
using System;
using System.Diagnostics;
using Sirenix.OdinInspector;
[AttributeUsage(AttributeTargets.All)]
[Conditional("UNITY_EDITOR")]
public class CBoxGroup : PropertyGroupAttribute
{
public float r, g, b, a;
public bool drawTitle;
public bool childrenInheritColor;
public CBoxGroup(string path, bool drawTitle = true, bool childrenInheritColor = false)
: base(path)
{
OdinColorHelper.GetColor(path, out r, out g, out b, out a);
this.drawTitle = drawTitle;
this.childrenInheritColor = childrenInheritColor;
}
public CBoxGroup(string path, string colorSource, bool drawTitle = true, bool childrenInheritColor = false)
: base(path)
{
OdinColorHelper.GetColor(colorSource, out r, out g, out b, out a);
this.drawTitle = drawTitle;
this.childrenInheritColor = childrenInheritColor;
}
public CBoxGroup(string path, float r, float g, float b, float a = 1f, bool drawTitle = true, bool childrenInheritColor = false)
: base(path)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
this.drawTitle = drawTitle;
this.childrenInheritColor = childrenInheritColor;
}
protected override void CombineValuesWith(PropertyGroupAttribute other)
{
var otherAttr = (CBoxGroup)other;
r = Math.Max(otherAttr.r, r);
g = Math.Max(otherAttr.g, g);
b = Math.Max(otherAttr.b, b);
a = Math.Max(otherAttr.a, a);
}
}
#endif
#if UNITY_2020_2_OR_NEWER && UNITY_EDITOR && ODIN_INSPECTOR
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEngine;
public class CBoxGroupAttributeDrawer : OdinGroupDrawer<CBoxGroup>
{
protected override void DrawPropertyLayout(GUIContent label)
{
GUIHelper.PushColor(new Color(Attribute.r, Attribute.g, Attribute.b, Attribute.a));
SirenixEditorGUI.BeginBox();
if (Attribute.drawTitle)
{
SirenixEditorGUI.BeginBoxHeader();
GUIHelper.PopColor();
SirenixEditorGUI.Title(label.text, "", TextAlignment.Left, false);
SirenixEditorGUI.EndBoxHeader();
}
else if (!Attribute.childrenInheritColor) GUIHelper.PopColor();
foreach (var child in Property.Children)
child.Draw();
if (Attribute.childrenInheritColor) GUIHelper.PopColor();
SirenixEditorGUI.EndBox();
}
}
#endif
#if UNITY_2020_2_OR_NEWER && ODIN_INSPECTOR
using System;
using System.Diagnostics;
using Sirenix.OdinInspector;
[AttributeUsage(AttributeTargets.All)]
[Conditional("UNITY_EDITOR")]
public class CFoldoutGroup : PropertyGroupAttribute
{
public float r, g, b, a;
public CFoldoutGroup(string path)
: base(path)
{
OdinColorHelper.GetColor(path, out r, out g, out b, out a);
}
public CFoldoutGroup(string path, string colorSource)
: base(path)
{
OdinColorHelper.GetColor(colorSource, out r, out g, out b, out a);
}
public CFoldoutGroup(string path, float r, float g, float b, float a = 1f)
: base(path)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
protected override void CombineValuesWith(PropertyGroupAttribute other)
{
var otherAttr = (CFoldoutGroup)other;
r = Math.Max(otherAttr.r, r);
g = Math.Max(otherAttr.g, g);
b = Math.Max(otherAttr.b, b);
a = Math.Max(otherAttr.a, a);
}
private static void GetColor(float hue, out float r, out float g, out float b, out float a)
{
var color = OdinColorHelper.ColorFromHSV(hue);
r = (float)color.R / 255;
g = (float)color.G / 255;
b = (float)color.B / 255;
a = 1;
}
}
#endif
#if UNITY_2020_2_OR_NEWER && UNITY_EDITOR && ODIN_INSPECTOR
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEngine;
public class CFoldoutGroupAttributeDrawer : OdinGroupDrawer<CFoldoutGroup>
{
private LocalPersistentContext<bool> isExpanded;
protected override void Initialize()
{
isExpanded = this.GetPersistentValue<bool>(
"ColoredFoldoutGroupAttributeDrawer.isExpanded",
GeneralDrawerConfig.Instance.ExpandFoldoutByDefault);
}
protected override void DrawPropertyLayout(GUIContent label)
{
GUIHelper.PushColor(new Color(Attribute.r, Attribute.g, Attribute.b, Attribute.a));
SirenixEditorGUI.BeginBox();
SirenixEditorGUI.BeginBoxHeader();
GUIHelper.PopColor();
isExpanded.Value = SirenixEditorGUI.Foldout(isExpanded.Value, label);
SirenixEditorGUI.EndBoxHeader();
if (SirenixEditorGUI.BeginFadeGroup(this, isExpanded.Value))
foreach (var child in Property.Children)
child.Draw();
SirenixEditorGUI.EndFadeGroup();
SirenixEditorGUI.EndBox();
}
}
#endif
#if UNITY_2020_2_OR_NEWER && ODIN_INSPECTOR
using System;
using System.Drawing;
public static class OdinColorHelper
{
public static Color ColorFromHSV(float hue, float saturation = 1, float value = 1)
{
var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
var f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
var v = Convert.ToInt32(value);
var p = Convert.ToInt32(value * (1 - saturation));
var q = Convert.ToInt32(value * (1 - f * saturation));
var t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
if (hi == 1)
return Color.FromArgb(255, q, v, p);
if (hi == 2)
return Color.FromArgb(255, p, v, t);
if (hi == 3)
return Color.FromArgb(255, p, q, v);
if (hi == 4)
return Color.FromArgb(255, t, p, v);
return Color.FromArgb(255, v, p, q);
}
public static void GetColor(string seed, out float r, out float g, out float b, out float a)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var color = ColorFromHSV(hue);
r = (float)color.R / 255;
g = (float)color.G / 255;
b = (float)color.B / 255;
a = 1;
}
public static UnityEngine.Color GetColor(string seed)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var systemColor = ColorFromHSV(hue);
return new UnityEngine.Color(
(float)systemColor.R / 255,
(float)systemColor.G / 255,
(float)systemColor.B / 255, 1);
}
public static float GetRed(string seed)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var color = ColorFromHSV(hue);
return (float)color.R / 255;
}
public static float GetGreen(string seed)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var color = ColorFromHSV(hue);
return (float)color.G / 255;
}
public static float GetBlue(string seed)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var color = ColorFromHSV(hue);
return (float)color.B / 255;
}
public static float GetAlpha(string seed)
{
var random = new Random(seed.GetHashCode());
var hue = (float)random.NextDouble() * 360;
var color = ColorFromHSV(hue);
return (float)color.B / 255;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment