Skip to content

Instantly share code, notes, and snippets.

@oxysoft
Last active August 26, 2023 11:33
Show Gist options
  • Save oxysoft/66fe16fd12f1402232e8a0c770f3a89e to your computer and use it in GitHub Desktop.
Save oxysoft/66fe16fd12f1402232e8a0c770f3a89e to your computer and use it in GitHub Desktop.
using System;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
using Random = System.Random;
/// <summary>
/// Draw the properties with a darker background and
/// borders, optionally.
/// </summary>
public class DarkBoxAttribute : Attribute
{
/// <summary>
/// Dark
/// </summary>
public readonly bool withBorders;
public DarkBoxAttribute()
{ }
public DarkBoxAttribute(bool withBorders)
{
this.withBorders = withBorders;
}
}
public class ColorBox : Attribute
{
}
namespace OdinExtensions
{
[DrawerPriority(0, 99)]
public class ColorBoxDrawer : OdinAttributeDrawer<ColorBox>
{
private Color _color;
private const float GOLDEN_RATIO = 0.618033988749895f;
protected override void DrawPropertyLayout(GUIContent label)
{
// float h = 0;
// for (int i = 0; i < Property.Index; i++)
// {
// h += GOLDEN_RATIO;
// while (h > 1) h -= 1;
// }
int hashCode = Property.ValueEntry.TypeOfValue.Name.GetHashCode();
var h = (float) ((hashCode + (double) int.MaxValue) / uint.MaxValue);
_color = Color.HSVToRGB(h, 0.95f, 0.75f);
_color.a = 0.15f;
BoxGUI.BeginBox(_color);
CallNextDrawer(label);
BoxGUI.EndBox();
}
}
#if UNITY_EDITOR
[DrawerPriority(0, 99)]
public class DarkBoxDrawer : OdinAttributeDrawer<DarkBoxAttribute>
{
public static readonly Color Color = EditorGUIUtility.isProSkin
? Color.Lerp(Color.black, Color.white, 0.1f)
: Color.gray;
protected override void DrawPropertyLayout(GUIContent label)
{
BoxGUI.BeginBox(new Color(0, 0, 0, 0.15f));
CallNextDrawer(label);
BoxGUI.EndBox(Attribute.withBorders ? Color : (Color?) null);
}
}
internal static class BoxGUI
{
private static Rect currentLayoutRect;
public static void BeginBox(Color color)
{
currentLayoutRect = EditorGUILayout.BeginVertical(SirenixGUIStyles.None);
// Rect currentLayoutRect = GUIHelper.GetCurrentLayoutRect();
if (Event.current.type == EventType.Repaint)
{
SirenixEditorGUI.DrawSolidRect(currentLayoutRect, color);
}
}
public static void EndBox(Color? borders = null)
{
EditorGUILayout.EndVertical();
if (Event.current.type == EventType.Repaint && borders != null)
{
SirenixEditorGUI.DrawBorders(currentLayoutRect, 1, 1, 1, 1, borders.Value);
}
GUILayout.Space(1);
}
}
}
#endif
@oxysoft
Copy link
Author

oxysoft commented Sep 13, 2020

Add this file to your project and begin using the attributes.

DarkContainer

Draws a dark box with optional borders.

image

ColorBox

Draws a colored box with an automatic color using the type's hashcode.

image

@digiwombat
Copy link

This file doesn't seem to do what it's supposed to. The DarkBoxAttribute() method doesn't even exist in it and there is not mention of color anywhere in the file.

@oxysoft
Copy link
Author

oxysoft commented Nov 10, 2020

Sorry, I think I mixed it up with another script. I will update this soon, sorry for the confusion.

@digiwombat
Copy link

No worries, thanks for putting it up to begin with. I just had to say something since I was really looking forward to implementing the Colorbox into some editors. :D

@Fuzionist1
Copy link

I think it still isn't updated with the correct script.

@xxl-game
Copy link

Any progress?

@oxysoft
Copy link
Author

oxysoft commented Jun 28, 2021

I finally got around to doing this, thanks to your reminder. The file was on my old annoying laptop, so I've been procrastinating and kept forgetting about it. Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment