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,11 +94,10 @@ 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; HotkeyRegistration hotkeyInfo;
@ -117,21 +108,16 @@ internal static class UXHotkeyComponent
hotkeyInfo = registrations[i]; hotkeyInfo = registrations[i];
registrations.RemoveAt(i); registrations.RemoveAt(i);
if (_sharedHandlers.TryGetValue(actionId, out var handlerInfo)) if (registrations.Count == 0 && _sharedHandlers.TryGetValue(actionId, out var handlerInfo))
{ {
var (handler, pressType) = handlerInfo; var (handler, actionRef) = handlerInfo;
var actionRef = hotkeyInfo.reference;
if (actionRef != null && actionRef.action != null) if (actionRef != null && actionRef.action != null)
{
if (registrations.Count == 0)
{ {
actionRef.action.Disable(); actionRef.action.Disable();
_sharedHandlers.Remove(actionId); _sharedHandlers.Remove(actionId);
_hotkeyRegistry.Remove(actionId); _hotkeyRegistry.Remove(actionId);
}
switch (pressType) switch (hotkeyInfo.pressType)
{ {
case EHotkeyPressType.Started: case EHotkeyPressType.Started:
actionRef.action.started -= handler; actionRef.action.started -= handler;
@ -147,7 +133,6 @@ internal static class UXHotkeyComponent
} }
} }
} }
}
_buttonRegistrations.Remove(button); _buttonRegistrations.Remove(button);