Update UXHotkeyRegisterManager.cs

This commit is contained in:
陈思海 2026-04-15 14:15:27 +08:00
parent 00a7eff5f8
commit 52eaadb67d

View File

@ -33,10 +33,12 @@ namespace UnityEngine.UI
{ {
Holder = holder; Holder = holder;
HierarchyDepth = GetHierarchyDepth(holder.transform); HierarchyDepth = GetHierarchyDepth(holder.transform);
BlocksLowerScopes = FindParentHolder(holder) == null; ParentHolder = FindParentHolder(holder);
BlocksLowerScopes = ParentHolder == null;
} }
public readonly UIHolderObjectBase Holder; public readonly UIHolderObjectBase Holder;
public readonly UIHolderObjectBase ParentHolder;
public readonly int HierarchyDepth; public readonly int HierarchyDepth;
public readonly bool BlocksLowerScopes; public readonly bool BlocksLowerScopes;
public readonly Dictionary<string, List<HotkeyRegistration>> RegistrationsByAction = new(StringComparer.Ordinal); public readonly Dictionary<string, List<HotkeyRegistration>> RegistrationsByAction = new(StringComparer.Ordinal);
@ -424,28 +426,30 @@ namespace UnityEngine.UI
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Dispatch(string actionId, EHotkeyPressType pressType) private static void Dispatch(string actionId, EHotkeyPressType pressType)
{ {
HotkeyScope[] leafScopes = GetLeafScopes(); HotkeyScope leafScope = GetTopLeafScope();
if (leafScopes.Length == 0) if (leafScope == null)
{ {
return; return;
} }
TryDispatchToScopeChain(leafScopes[0], actionId, pressType); TryDispatchToScopeChain(leafScope, actionId, pressType);
} }
private static bool TryDispatchToScopeChain(HotkeyScope leafScope, string actionId, EHotkeyPressType pressType) private static bool TryDispatchToScopeChain(HotkeyScope leafScope, string actionId, EHotkeyPressType pressType)
{ {
UIHolderObjectBase currentHolder = leafScope.Holder; HotkeyScope current = leafScope;
while (currentHolder != null) while (current != null)
{ {
if (_scopes.TryGetValue(currentHolder, out var scope) if (TryGetLatestRegistration(current, actionId, pressType, out var registration))
&& TryGetLatestRegistration(scope, actionId, pressType, out var registration))
{ {
registration.Trigger?.HotkeyActionTrigger(); registration.Trigger?.HotkeyActionTrigger();
return true; return true;
} }
currentHolder = FindParentHolder(currentHolder); UIHolderObjectBase parentHolder = current.ParentHolder;
current = parentHolder != null && _scopes.TryGetValue(parentHolder, out var parentScope)
? parentScope
: null;
} }
return false; return false;
@ -470,7 +474,7 @@ namespace UnityEngine.UI
return false; return false;
} }
private static HotkeyScope[] GetLeafScopes() private static HotkeyScope GetTopLeafScope()
{ {
_leafScopes.Clear(); _leafScopes.Clear();
_ancestorHolders.Clear(); _ancestorHolders.Clear();
@ -486,11 +490,18 @@ namespace UnityEngine.UI
continue; continue;
} }
UIHolderObjectBase parentHolder = FindParentHolder(scope.Holder); UIHolderObjectBase parentHolder = scope.ParentHolder;
while (parentHolder != null) while (parentHolder != null)
{ {
_ancestorHolders.Add(parentHolder); _ancestorHolders.Add(parentHolder);
parentHolder = FindParentHolder(parentHolder); if (_scopes.TryGetValue(parentHolder, out var parentScope))
{
parentHolder = parentScope.ParentHolder;
}
else
{
break;
}
} }
} }
@ -506,8 +517,13 @@ namespace UnityEngine.UI
} }
} }
if (_leafScopes.Count == 0)
{
return null;
}
_leafScopes.Sort(CompareScopePriority); _leafScopes.Sort(CompareScopePriority);
return _leafScopes.ToArray(); return _leafScopes[0];
} }
private static bool IsScopeActive(HotkeyScope scope) private static bool IsScopeActive(HotkeyScope scope)