From f970e2df5fc10f27fd8506aa065f85e24c7a090b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Mon, 11 Aug 2025 16:31:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/UXComponent/UX/UXHotkey.cs | 79 ++++++++++++------------------ 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/Runtime/UXComponent/UX/UXHotkey.cs b/Runtime/UXComponent/UX/UXHotkey.cs index 330335b..1032b08 100644 --- a/Runtime/UXComponent/UX/UXHotkey.cs +++ b/Runtime/UXComponent/UX/UXHotkey.cs @@ -14,14 +14,12 @@ internal static class UXHotkeyComponent { private readonly struct HotkeyRegistration { - public readonly InputActionReference reference; public readonly EHotkeyPressType pressType; public readonly UXButton button; - public HotkeyRegistration(UXButton btn, InputActionReference reference, EHotkeyPressType pressType) + public HotkeyRegistration(UXButton btn, EHotkeyPressType pressType) { button = btn; - this.reference = reference; this.pressType = pressType; } } @@ -30,12 +28,12 @@ internal static class UXHotkeyComponent new Dictionary>(32); - private static readonly Dictionary handler, EHotkeyPressType pressType)> _sharedHandlers = - new Dictionary, EHotkeyPressType)>(32); + private static readonly Dictionary handler, InputActionReference action)> _sharedHandlers = + new Dictionary, InputActionReference)>(32); - private static readonly Dictionary> _buttonRegistrations = - new Dictionary>(64); + private static readonly Dictionary _buttonRegistrations = + new Dictionary(64); #if UNITY_EDITOR @@ -61,7 +59,7 @@ internal static class UXHotkeyComponent string actionId = action.action.id.ToString(); - HotkeyRegistration registration = new HotkeyRegistration(button, action, pressType); + HotkeyRegistration registration = new HotkeyRegistration(button, pressType); if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations)) @@ -73,18 +71,12 @@ internal static class UXHotkeyComponent registrations.Add(registration); - if (!_buttonRegistrations.TryGetValue(button, out var actionIds)) - { - actionIds = new HashSet(); - _buttonRegistrations[button] = actionIds; - } - - actionIds.Add(actionId); + _buttonRegistrations[button] = actionId; if (!_sharedHandlers.ContainsKey(actionId)) { System.Action handler = ctx => OnHotkeyTriggered(actionId); - _sharedHandlers[actionId] = (handler, pressType); + _sharedHandlers[actionId] = (handler, action); switch (pressType) { @@ -102,49 +94,42 @@ internal static class UXHotkeyComponent public static void UnregisterHotkey(UXButton button) { - if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionIds)) + if (button == null || !_buttonRegistrations.TryGetValue(button, out var actionId)) return; - foreach (var actionId in actionIds) + + if (_hotkeyRegistry.TryGetValue(actionId, out var registrations)) { - if (_hotkeyRegistry.TryGetValue(actionId, out var registrations)) + HotkeyRegistration hotkeyInfo; + for (int i = registrations.Count - 1; i >= 0; i--) { - HotkeyRegistration hotkeyInfo; - for (int i = registrations.Count - 1; i >= 0; i--) + if (registrations[i].button == button) { - if (registrations[i].button == button) + hotkeyInfo = registrations[i]; + registrations.RemoveAt(i); + + if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo)) { - hotkeyInfo = registrations[i]; - registrations.RemoveAt(i); - - if (_sharedHandlers.TryGetValue(actionId, out var handlerInfo)) + var (handler, actionRef) = handlerInfo; + if (actionRef != null && actionRef.action != null) { - var (handler, pressType) = handlerInfo; - var actionRef = hotkeyInfo.reference; + actionRef.action.Disable(); + _sharedHandlers.Remove(actionId); + _hotkeyRegistry.Remove(actionId); - if (actionRef != null && actionRef.action != null) + switch (hotkeyInfo.pressType) { - if (registrations.Count == 0) - { - actionRef.action.Disable(); - _sharedHandlers.Remove(actionId); - _hotkeyRegistry.Remove(actionId); - } - - switch (pressType) - { - case EHotkeyPressType.Started: - actionRef.action.started -= handler; - break; - case EHotkeyPressType.Performed: - actionRef.action.performed -= handler; - break; - } + case EHotkeyPressType.Started: + actionRef.action.started -= handler; + break; + case EHotkeyPressType.Performed: + actionRef.action.performed -= handler; + break; } } - - break; } + + break; } } }