com.alicizax.unity.ui.exten.../Editor/UX/Hotkey/HotkeyBindComponentInspector.cs
陈思海 551423f09d 大优化
大优化 懒得写了 重构了分离了 UXButton UXToggle UXSelectable UXGroup
2025-12-19 20:26:22 +08:00

100 lines
3.3 KiB
C#

#if INPUTSYSTEM_SUPPORT
using System.Linq;
using System.Reflection;
using AlicizaX.UI.Runtime;
using UnityEngine;
using UnityEngine.UI;
namespace UnityEditor.UI
{
[CustomEditor(typeof(HotkeyBindComponent))]
public class HotkeyBindComponentInspector : UnityEditor.Editor
{
private SerializedProperty hotButtonsProp;
private HotkeyBindComponent _target;
private void OnEnable()
{
_target = (HotkeyBindComponent)target;
hotButtonsProp = serializedObject.FindProperty("hotButtons");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
var holder = _target.GetComponent<UIHolderObjectBase>();
if (holder == null)
{
EditorGUILayout.HelpBox(
"⚠ 当前对象缺少 UIHolderObjectBase 组件。\nHotkeyBindComponent 依赖它进行热键绑定事件。",
MessageType.Error
);
}
EditorGUILayout.Space();
// 灰掉显示 hotButtons 列表(不可手动编辑)
GUI.enabled = false;
DrawHotButtonListAlwaysExpanded(hotButtonsProp);
GUI.enabled = true;
EditorGUILayout.Space();
// 按钮:扫描子物体(包含隐藏)
if (GUILayout.Button("🔍 扫描所有子物体 (包含隐藏对象)"))
{
FindAllUXHotkeys();
}
serializedObject.ApplyModifiedProperties();
}
/// <summary>
/// 自定义展开显示 hotButtons 列表
/// </summary>
private void DrawHotButtonListAlwaysExpanded(SerializedProperty listProp)
{
EditorGUILayout.LabelField("Hot Buttons", EditorStyles.boldLabel);
if (listProp == null || listProp.arraySize == 0)
{
EditorGUILayout.HelpBox("当前没有绑定任何 UXHotkey。", MessageType.Info);
return;
}
EditorGUI.indentLevel++;
for (int i = 0; i < listProp.arraySize; i++)
{
var element = listProp.GetArrayElementAtIndex(i);
var comp = element.objectReferenceValue as Component;
string name = comp != null ? comp.name + " (" + comp.GetType().Name + ")" : "Null";
// 注意:这里用 typeof(Component)
EditorGUILayout.ObjectField($"[{i}] {name}", element.objectReferenceValue, typeof(Component), true);
}
EditorGUI.indentLevel--;
}
/// <summary>
/// 查找所有子物体(包含隐藏)并绑定 UXHotkey
/// </summary>
private void FindAllUXHotkeys()
{
Undo.RecordObject(_target, "Scan UXHotkey");
var collectMethod = target.GetType().GetMethod("CollectUXHotkeys", BindingFlags.NonPublic | BindingFlags.Instance);
if (collectMethod != null)
{
collectMethod.Invoke(target, null);
EditorUtility.SetDirty(_target);
serializedObject.Update();
}
else
{
Debug.LogWarning("未找到 CollectUXHotkeys 方法。");
}
}
}
}
#endif