com.alicizax.unity.ui.exten.../Editor/UX/Hotkey/HotkeyBindComponentInspector.cs

112 lines
3.7 KiB
C#
Raw Normal View History

2025-10-13 20:20:01 +08:00
#if INPUTSYSTEM_SUPPORT
using System.Collections.Generic;
using System.Reflection;
using AlicizaX.Editor;
using AlicizaX.UI.Extension;
using AlicizaX.UI.Extension.UXComponent.Hotkey;
using AlicizaX.UI.Runtime;
using UnityEditor;
using UnityEngine;
namespace UnityEngine.UI.Hotkey
{
[CustomEditor(typeof(HotkeyBindComponent))]
public class HotkeyBindComponentInspector : GameFrameworkInspector
{
private SerializedProperty hotButtonsProp;
private HotkeyBindComponent _target;
private void OnEnable()
{
_target = (HotkeyBindComponent)target;
hotButtonsProp = serializedObject.FindProperty("hotButtons");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
// 🔸 检查是否缺少 UIHolderObjectBase
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.arraySize == 0)
{
EditorGUILayout.HelpBox("当前没有绑定任何 UXHotkey。", MessageType.Info);
return;
}
EditorGUI.indentLevel++;
for (int i = 0; i < listProp.arraySize; i++)
{
var element = listProp.GetArrayElementAtIndex(i);
var uxHotkey = element.objectReferenceValue as UXHotkey;
string name = uxHotkey != null ? uxHotkey.name : "Null";
EditorGUILayout.ObjectField($"[{i}] {name}", element.objectReferenceValue, typeof(UXHotkey), true);
}
EditorGUI.indentLevel--;
}
/// <summary>
/// 查找所有子物体(包含隐藏)并绑定 UXHotkey
/// </summary>
private void FindAllUXHotkeys()
{
Undo.RecordObject(_target, "Scan UXHotkey");
var uxHotkeys = _target.GetComponentsInChildren<UXHotkey>(true);
List<UXHotkey> valiedHotkeys = new List<UXHotkey>();
foreach (var item in uxHotkeys)
{
var field = item.GetType()
.GetField("_hotKeyRefrence", BindingFlags.NonPublic | BindingFlags.Instance);
var hotRefrenceObject = field.GetValue(item);
if (hotRefrenceObject != null) valiedHotkeys.Add(item);
}
_target.GetType()
.GetField("hotButtons", BindingFlags.NonPublic | BindingFlags.Instance)
?.SetValue(_target, valiedHotkeys.ToArray());
EditorUtility.SetDirty(_target);
serializedObject.Update();
Debug.Log($"[HotkeyBindComponent] 已找到 {uxHotkeys.Length} 个 UXHotkey 组件并绑定。");
}
}
}
#endif