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

100 lines
3.3 KiB
C#
Raw Normal View History

2025-10-13 20:20:01 +08:00
#if INPUTSYSTEM_SUPPORT
using System.Linq;
2025-10-13 20:20:01 +08:00
using System.Reflection;
using AlicizaX.UI.Runtime;
using UnityEngine;
using UnityEngine.UI;
2025-10-13 20:20:01 +08:00
namespace UnityEditor.UI
2025-10-13 20:20:01 +08:00
{
[CustomEditor(typeof(HotkeyBindComponent))]
2025-12-10 11:11:59 +08:00
public class HotkeyBindComponentInspector : UnityEditor.Editor
2025-10-13 20:20:01 +08:00
{
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 列表(不可手动编辑)
2025-10-13 20:20:01 +08:00
GUI.enabled = false;
DrawHotButtonListAlwaysExpanded(hotButtonsProp);
GUI.enabled = true;
EditorGUILayout.Space();
// 按钮:扫描子物体(包含隐藏)
2025-10-13 20:20:01 +08:00
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)
2025-10-13 20:20:01 +08:00
{
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);
2025-10-13 20:20:01 +08:00
}
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)
2025-10-13 20:20:01 +08:00
{
collectMethod.Invoke(target, null);
EditorUtility.SetDirty(_target);
serializedObject.Update();
}
else
{
Debug.LogWarning("未找到 CollectUXHotkeys 方法。");
2025-10-13 20:20:01 +08:00
}
}
}
}
#endif