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
{
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<string, List<HotkeyRegistration>>(32);
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, EHotkeyPressType pressType)> _sharedHandlers =
new Dictionary<string, (System.Action<InputAction.CallbackContext>, EHotkeyPressType)>(32);
private static readonly Dictionary<string, (System.Action<InputAction.CallbackContext> handler, InputActionReference action)> _sharedHandlers =
new Dictionary<string, (System.Action<InputAction.CallbackContext>, InputActionReference)>(32);
private static readonly Dictionary<UXButton, HashSet<string>> _buttonRegistrations =
new Dictionary<UXButton, HashSet<string>>(64);
private static readonly Dictionary<UXButton, string> _buttonRegistrations =
new Dictionary<UXButton, string>(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<string>();
_buttonRegistrations[button] = actionIds;
}
actionIds.Add(actionId);
_buttonRegistrations[button] = actionId;
if (!_sharedHandlers.ContainsKey(actionId))
{
System.Action<InputAction.CallbackContext> handler = ctx => OnHotkeyTriggered(actionId);
_sharedHandlers[actionId] = (handler, pressType);
_sharedHandlers[actionId] = (handler, action);
switch (pressType)
{
@ -102,11 +94,10 @@ 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))
{
HotkeyRegistration hotkeyInfo;
@ -117,21 +108,16 @@ internal static class UXHotkeyComponent
hotkeyInfo = registrations[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 actionRef = hotkeyInfo.reference;
var (handler, actionRef) = handlerInfo;
if (actionRef != null && actionRef.action != null)
{
if (registrations.Count == 0)
{
actionRef.action.Disable();
_sharedHandlers.Remove(actionId);
_hotkeyRegistry.Remove(actionId);
}
switch (pressType)
switch (hotkeyInfo.pressType)
{
case EHotkeyPressType.Started:
actionRef.action.started -= handler;
@ -147,7 +133,6 @@ internal static class UXHotkeyComponent
}
}
}
}
_buttonRegistrations.Remove(button);