#if INPUTSYSTEM_SUPPORT using System.Linq; using AlicizaX.UI.Runtime; using UnityEngine; namespace UnityEngine.UI { public class HotkeyBindComponent : MonoBehaviour { private UIHolderObjectBase _holderObjectBase; private void Awake() { _holderObjectBase = GetComponent(); _holderObjectBase.OnWindowBeforeShowEvent += BindHotKeys; _holderObjectBase.OnWindowBeforeClosedEvent += UnBindHotKeys; } private void OnDestroy() { if (_holderObjectBase != null) { _holderObjectBase.OnWindowBeforeShowEvent -= BindHotKeys; _holderObjectBase.OnWindowBeforeClosedEvent -= UnBindHotKeys; } } // 改成 Component[](或 MonoBehaviour[]),Unity 可以序列化 Component 引用 [SerializeField] private Component[] hotButtons; internal void BindHotKeys() { if (hotButtons == null) return; for (int i = 0; i < hotButtons.Length; i++) { if (hotButtons[i] is IHotkeyTrigger trigger) { trigger.BindHotKey(); } } } #if UNITY_EDITOR [ContextMenu("Bind HotKeys")] private void CollectUXHotkeys() { // 更稳健的查找:先拿所有 MonoBehaviour,再 OfType 接口 var found = gameObject .GetComponentsInChildren(true) .OfType() .Where(t => t.HotkeyAction != null) // 保留原来的筛选条件(如果 HotkeyAction 存在) .Select(t => t as Component) .ToArray(); hotButtons = found; Debug.Log($"[HotkeyBindComponent] 已找到 {hotButtons.Length} 个 UXHotkey 组件并绑定。"); } private void OnValidate() { if (_holderObjectBase == null) { _holderObjectBase = gameObject.GetComponent(); } // 在编辑器模式下自动收集(可根据需求移除) CollectUXHotkeys(); } #endif internal void UnBindHotKeys() { if (hotButtons == null) return; for (int i = 0; i < hotButtons.Length; i++) { if (hotButtons[i] is IHotkeyTrigger trigger) { trigger.UnBindHotKey(); } } } } } #endif