This commit is contained in:
陈思海 2025-08-11 16:31:50 +08:00
parent d840d92225
commit f970e2df5f

View File

@ -14,14 +14,12 @@ internal static class UXHotkeyComponent
{ {
private readonly struct HotkeyRegistration private readonly struct HotkeyRegistration
{ {
public readonly InputActionReference reference;
public readonly EHotkeyPressType pressType; public readonly EHotkeyPressType pressType;
public readonly UXButton button; public readonly UXButton button;
public HotkeyRegistration(UXButton btn, InputActionReference reference, EHotkeyPressType pressType) public HotkeyRegistration(UXButton btn, EHotkeyPressType pressType)
{ {
button = btn; button = btn;
this.reference = reference;
this.pressType = pressType; this.pressType = pressType;
} }
} }
@ -30,12 +28,12 @@ internal static class UXHotkeyComponent
new Dictionary<string, List<HotkeyRegistration>>(32); new Dictionary<string, List<HotkeyRegistration>>(32);
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, EHotkeyPressType pressType)> _sharedHandlers = private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
new Dictionary<string, (System.Action<InputAction.CallbackContext>, EHotkeyPressType)>(32); new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32);
private static readonly Dictionary<UXButton, HashSet<string>> _buttonRegistrations = private static readonly Dictionary<UXButton, string> _buttonRegistrations =
new Dictionary<UXButton, HashSet<string>>(64); new Dictionary<UXButton, string>(64);
#if UNITY_EDITOR #if UNITY_EDITOR
@ -61,7 +59,7 @@ internal static class UXHotkeyComponent
string actionId = action.action.id.ToString(); 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)) if (!_hotkeyRegistry.TryGetValue(actionId, out var registrations))
@ -73,18 +71,12 @@ internal static class UXHotkeyComponent
registrations.Add(registration); registrations.Add(registration);
if (!_buttonRegistrations.TryGetValue(button, out var actionIds)) _buttonRegistrations[button] = actionId;
{
actionIds = new HashSet<string>();
_buttonRegistrations[button] = actionIds;
}
actionIds.Add(actionId);
if (!_sharedHandlers.ContainsKey(actionId)) if (!_sharedHandlers.ContainsKey(actionId))
{ {
System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId); System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
_sharedHandlers[actionId] = (handler, pressType); _sharedHandlers[actionId] = (handler, action);
switch (pressType) switch (pressType)
{ {
@ -102,49 +94,42 @@ internal static class UXHotkeyComponent
public static void UnregisterHotkey(UXButton button) 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; 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; if (registrations[i].button == button)
for (int i = registrations.Count - 1; i >= 0; i--)
{ {
if (registrations[i].button == button) hotkeyInfo = registrations[i];
registrations.RemoveAt(i);
if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
{ {
hotkeyInfo = registrations[i]; var (handler, actionRef) = handlerInfo;
registrations.RemoveAt(i); if (actionRef != null && actionRef.action != null)
if (_sharedHandlers.TryGetValue(actionId, out var handlerInfo))
{ {
var (handler, pressType) = handlerInfo; actionRef.action.Disable();
var actionRef = hotkeyInfo.reference; _sharedHandlers.Remove(actionId);
_hotkeyRegistry.Remove(actionId);
if (actionRef != null && actionRef.action != null) switch (hotkeyInfo.pressType)
{ {
if (registrations.Count == 0) case EHotkeyPressType.Started:
{ actionRef.action.started -= handler;
actionRef.action.Disable(); break;
_sharedHandlers.Remove(actionId); case EHotkeyPressType.Performed:
_hotkeyRegistry.Remove(actionId); actionRef.action.performed -= handler;
} break;
switch (pressType)
{
case EHotkeyPressType.Started:
actionRef.action.started -= handler;
break;
case EHotkeyPressType.Performed:
actionRef.action.performed -= handler;
break;
}
} }
} }
break;
} }
break;
} }
} }
} }