UnityExplorer/src/Helpers/UIHelpers.cs

118 lines
3.5 KiB
C#
Raw Normal View History

2020-08-07 22:19:03 +10:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using UnhollowerRuntimeLib;
2020-08-07 22:19:03 +10:00
using UnityEngine;
using Object = UnityEngine.Object;
namespace Explorer
{
public class UIHelpers
2020-08-07 22:19:03 +10:00
{
// helper for "Instantiate" button on UnityEngine.Objects
public static void InstantiateButton(Object obj, float width = 100)
{
if (GUILayout.Button("Instantiate", new GUILayoutOption[] { GUILayout.Width(width) }))
{
var newobj = Object.Instantiate(obj);
WindowManager.InspectObject(newobj, out _);
}
}
// helper for drawing a styled button for a GameObject or Transform
public static void GOButton(object _obj, Action<Transform> specialInspectMethod = null, bool showSmallInspectBtn = true, float width = 380)
2020-08-07 22:19:03 +10:00
{
var obj = (_obj as GameObject) ?? (_obj as Transform).gameObject;
bool hasChild = obj.transform.childCount > 0;
2020-08-07 22:19:03 +10:00
string label = hasChild ? $"[{obj.transform.childCount} children] " : "";
2020-08-07 22:19:03 +10:00
label += obj.name;
bool enabled = obj.activeSelf;
int childCount = obj.transform.childCount;
Color color;
2020-08-07 22:19:03 +10:00
if (enabled)
{
if (childCount > 0)
2020-08-07 22:19:03 +10:00
{
color = Color.green;
2020-08-07 22:19:03 +10:00
}
else
{
color = UIStyles.LightGreen;
2020-08-07 22:19:03 +10:00
}
}
else
{
color = Color.red;
}
GOButton_Impl(_obj, color, label, obj.activeSelf, specialInspectMethod, showSmallInspectBtn, width);
}
public static void GOButton_Impl(object _obj, Color activeColor, string label, bool enabled, Action<Transform> specialInspectMethod = null, bool showSmallInspectBtn = true, float width = 380)
{
var obj = _obj as GameObject ?? (_obj as Transform).gameObject;
if (!obj)
{
GUILayout.Label("<i><color=red>null</color></i>", null);
return;
2020-08-07 22:19:03 +10:00
}
// ------ toggle active button ------
GUILayout.BeginHorizontal(null);
GUI.skin.button.alignment = TextAnchor.UpperLeft;
GUI.color = activeColor;
2020-08-07 22:19:03 +10:00
enabled = GUILayout.Toggle(enabled, "", new GUILayoutOption[] { GUILayout.Width(18) });
if (obj.activeSelf != enabled)
{
obj.SetActive(enabled);
}
// ------- actual button ---------
if (GUILayout.Button(label, new GUILayoutOption[] { GUILayout.Height(22), GUILayout.Width(width) }))
{
if (specialInspectMethod != null)
{
specialInspectMethod(obj.transform);
2020-08-07 22:19:03 +10:00
}
else
{
WindowManager.InspectObject(_obj, out bool _);
2020-08-07 22:19:03 +10:00
}
}
// ------ small "Inspect" button on the right ------
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
GUI.color = Color.white;
if (showSmallInspectBtn)
{
SmallInspectButton(_obj);
2020-08-07 22:19:03 +10:00
}
GUILayout.EndHorizontal();
}
public static void SmallInspectButton(object obj)
{
if (GUILayout.Button("Inspect", null))
{
WindowManager.InspectObject(obj, out bool _);
}
}
2020-08-07 22:19:03 +10:00
}
}