2023-03-27 20:31:58 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2023-05-07 00:50:44 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
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
|
|
|
|
{
|
|
|
|
|
public static class EcsEditor
|
|
|
|
|
{
|
2023-05-07 00:50:44 +08:00
|
|
|
|
private static SparseArray<GUIStyle> colorBoxeStyles = new SparseArray<GUIStyle>();
|
2023-03-30 20:49:10 +08:00
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
if (style == null)
|
|
|
|
|
style = CreateStyle(color32, colorCode);
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetGenericName(Type type)
|
|
|
|
|
{
|
|
|
|
|
string friendlyName = type.Name;
|
|
|
|
|
if (type.IsGenericType)
|
|
|
|
|
{
|
|
|
|
|
int iBacktick = friendlyName.IndexOf('`');
|
|
|
|
|
if (iBacktick > 0)
|
|
|
|
|
friendlyName = friendlyName.Remove(iBacktick);
|
|
|
|
|
|
|
|
|
|
friendlyName += "<";
|
|
|
|
|
Type[] typeParameters = type.GetGenericArguments();
|
|
|
|
|
for (int i = 0; i < typeParameters.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
string typeParamName = GetGenericName(typeParameters[i]);
|
|
|
|
|
friendlyName += (i == 0 ? typeParamName : "," + typeParamName);
|
|
|
|
|
}
|
|
|
|
|
friendlyName += ">";
|
|
|
|
|
}
|
|
|
|
|
return friendlyName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetName<T>() => GetName(typeof(T));
|
|
|
|
|
public static string GetName(Type type)
|
|
|
|
|
{
|
|
|
|
|
var atr = type.GetCustomAttribute<DebugNameAttribute>();
|
|
|
|
|
return atr != null ? atr.name : GetGenericName(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetDescription<T>() => GetDescription(typeof(T));
|
|
|
|
|
public static string GetDescription(Type type)
|
|
|
|
|
{
|
|
|
|
|
var atr = type.GetCustomAttribute<DebugDescriptionAttribute>();
|
|
|
|
|
return atr != null ? atr.description : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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
|