Files
Ultimate-Fishing-Simulator-…/Assets/Plugins/Assembly-CSharp-firstpass/DebuggingEssentials/DrawEnum.cs
2026-03-04 09:37:33 +08:00

160 lines
3.3 KiB
C#

using System;
using System.Reflection;
using UnityEngine;
namespace DebuggingEssentials
{
public class DrawEnum
{
public static bool draw;
private const float heightPadding = 4f;
private const float addHeight = 2f;
private float boxAlpha;
private float backgroundAlpha;
private Enum enumField;
private GameObject go;
private object obj;
private MemberInfo member;
private GUISkin skin;
private Rect rect;
private string[] names;
public static void ResetStatic()
{
draw = false;
}
public DrawEnum(GUISkin skin)
{
this.skin = skin;
}
public void DrawButton(object obj, string fieldName)
{
GUILayout.Button(obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(obj)
.ToString());
if (Event.current.type == EventType.Repaint && !draw)
{
rect = GUILayoutUtility.GetLastRect();
}
}
public void InitEnumWindow(object obj, MemberInfo member, Enum enumField, Vector2 pos)
{
go = null;
this.obj = obj;
this.member = member;
this.enumField = enumField;
names = Enum.GetNames(enumField.GetType());
rect.position = pos;
rect.height = 0f;
float num = 0f;
for (int i = 0; i < names.Length; i++)
{
Vector2 vector = GUI.skin.button.CalcSize(new GUIContent(names[i]));
if (vector.x > num)
{
num = vector.x;
}
rect.height += vector.y + 4f;
}
rect.height += 2f;
rect.width = num + 10f;
if (names != null)
{
draw = true;
}
}
public void InitEnumWindow(GameObject go, Vector2 pos)
{
this.go = go;
rect.position = pos;
rect.height = 0f;
float num = 0f;
FastList<string> fastList = new FastList<string>();
for (int i = 0; i < 32; i++)
{
string text = LayerMask.LayerToName(i);
if (text != string.Empty)
{
Vector2 vector = GUI.skin.button.CalcSize(new GUIContent(text));
fastList.Add(text);
if (vector.x > num)
{
num = vector.x;
}
rect.height += vector.y + 4f;
}
}
rect.height += 2f;
rect.width = num + 10f;
names = fastList.ToArray();
draw = true;
}
public void Draw(float boxAlpha, float backgroundAlpha)
{
if (draw)
{
this.boxAlpha = boxAlpha;
this.backgroundAlpha = backgroundAlpha;
GUI.skin = skin;
GUILayout.Window(3443, rect, DrawWindow, GUIContent.none);
if (Event.current.type == EventType.MouseDown)
{
draw = false;
}
}
}
private void DrawWindow(int windowId)
{
GUI.BringWindowToFront(windowId);
GUI.backgroundColor = new Color(0f, 0f, 0f, boxAlpha);
GUILayout.BeginVertical("Box");
GUI.backgroundColor = new Color(1f, 1f, 1f, backgroundAlpha);
GUILayout.Space(-1f);
for (int i = 0; i < names.Length; i++)
{
if (!GUILayout.Button(names[i]))
{
continue;
}
draw = false;
if (go != null)
{
go.layer = LayerMask.NameToLayer(names[i]);
go = null;
continue;
}
FieldInfo fieldInfo = member as FieldInfo;
if (fieldInfo != null)
{
fieldInfo.SetValue(obj, i);
break;
}
PropertyInfo propertyInfo = member as PropertyInfo;
if (propertyInfo != null)
{
propertyInfo.SetValue(obj, Enum.Parse(enumField.GetType(), names[i]), null);
}
obj = null;
member = null;
enumField = null;
}
}
}
}