DragonECS-Unity/src/Debug/Editor/EcsEditor.cs

103 lines
3.4 KiB
C#
Raw Normal View History

2023-03-27 20:31:58 +08:00
#if UNITY_EDITOR
2023-05-07 00:50:44 +08:00
using System;
using System.Runtime.InteropServices;
2023-05-27 09:14:36 +08:00
using UnityEditor;
2023-03-27 20:31:58 +08:00
using UnityEngine;
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS.Editors
2023-03-27 20:31:58 +08:00
{
2023-05-27 09:14:36 +08:00
[InitializeOnLoad]
2023-03-27 20:31:58 +08:00
public static class EcsEditor
{
2023-05-27 09:14:36 +08:00
static EcsEditor()
{
colorBoxeStyles = new SparseArray<GUIStyle>();
}
2023-05-07 00:50:44 +08:00
private static SparseArray<GUIStyle> colorBoxeStyles = new SparseArray<GUIStyle>();
public static GUIStyle GetStyle(Color color, float alphaMultiplier)
2023-03-27 20:31:58 +08:00
{
2023-05-07 00:50:44 +08:00
color.a *= alphaMultiplier;
return GetStyle(color);
}
public static GUIStyle GetStyle(Color32 color32)
{
int colorCode = new Color32Union(color32).colorCode;
if (colorBoxeStyles.TryGetValue(colorCode, out GUIStyle style))
{
2023-05-27 09:14:36 +08:00
if (style == null || style.normal.background == null)
{
2023-05-07 00:50:44 +08:00
style = CreateStyle(color32, colorCode);
2023-05-27 09:14:36 +08:00
colorBoxeStyles[colorCode] = style;
}
2023-05-07 00:50:44 +08:00
return style;
}
2023-03-27 20:31:58 +08:00
2023-05-07 00:50:44 +08:00
style = CreateStyle(color32, colorCode);
colorBoxeStyles.Add(colorCode, style);
2023-03-27 20:31:58 +08:00
return style;
}
2023-05-07 00:50:44 +08:00
private static GUIStyle CreateStyle(Color32 color32, int colorCode)
{
GUIStyle result = new GUIStyle(GUI.skin.box);
Color componentColor = color32;
result.normal.background = CreateTexture(2, 2, componentColor);
2023-05-27 09:14:36 +08:00
result.active.background = CreateTexture(2, 2, componentColor);
result.hover.background = CreateTexture(2, 2, componentColor);
result.focused.background = CreateTexture(2, 2, componentColor);
2023-05-07 00:50:44 +08:00
return result;
}
2023-03-27 20:31:58 +08:00
private static Texture2D CreateTexture(int width, int height, Color color)
{
var pixels = new Color[width * height];
for (var i = 0; i < pixels.Length; ++i)
pixels[i] = color;
var result = new Texture2D(width, height);
result.SetPixels(pixels);
result.Apply();
return result;
}
2023-05-07 00:50:44 +08:00
2023-05-23 01:48:54 +08:00
public static string GetGenericName(Type type) => EcsDebugUtility.GetGenericTypeName(type);
2023-05-07 00:50:44 +08:00
public static string GetName<T>() => GetName(typeof(T));
2023-05-23 01:48:54 +08:00
public static string GetName(Type type) => EcsDebugUtility.GetName(type);
2023-05-07 00:50:44 +08:00
public static string GetDescription<T>() => GetDescription(typeof(T));
2023-05-23 01:48:54 +08:00
public static string GetDescription(Type type) => EcsDebugUtility.GetDescription(type);
2023-05-07 00:50:44 +08:00
#region Utils
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)]
private readonly ref struct Color32Union
{
[FieldOffset(0)]
public readonly int colorCode;
[FieldOffset(0)]
public readonly byte r;
[FieldOffset(1)]
public readonly byte g;
[FieldOffset(2)]
public readonly byte b;
[FieldOffset(3)]
public readonly byte a;
public Color32Union(byte r, byte g, byte b, byte a) : this()
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public Color32Union(Color32 color) : this()
{
r = color.r;
g = color.g;
b = color.b;
a = color.a;
}
}
#endregion
2023-03-27 20:31:58 +08:00
}
}
#endif