From 52eaadb67db6f56bb817e8ab0fe4f4f34744fe17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Wed, 15 Apr 2026 14:15:27 +0800 Subject: [PATCH] Update UXHotkeyRegisterManager.cs --- .../Hotkey/UXHotkeyRegisterManager.cs | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs b/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs index 40dbf69..0cd1921 100644 --- a/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs +++ b/Runtime/UXComponent/Hotkey/UXHotkeyRegisterManager.cs @@ -33,10 +33,12 @@ namespace UnityEngine.UI { Holder = holder; HierarchyDepth = GetHierarchyDepth(holder.transform); - BlocksLowerScopes = FindParentHolder(holder) == null; + ParentHolder = FindParentHolder(holder); + BlocksLowerScopes = ParentHolder == null; } public readonly UIHolderObjectBase Holder; + public readonly UIHolderObjectBase ParentHolder; public readonly int HierarchyDepth; public readonly bool BlocksLowerScopes; public readonly Dictionary> RegistrationsByAction = new(StringComparer.Ordinal); @@ -424,28 +426,30 @@ namespace UnityEngine.UI [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Dispatch(string actionId, EHotkeyPressType pressType) { - HotkeyScope[] leafScopes = GetLeafScopes(); - if (leafScopes.Length == 0) + HotkeyScope leafScope = GetTopLeafScope(); + if (leafScope == null) { return; } - TryDispatchToScopeChain(leafScopes[0], actionId, pressType); + TryDispatchToScopeChain(leafScope, actionId, pressType); } private static bool TryDispatchToScopeChain(HotkeyScope leafScope, string actionId, EHotkeyPressType pressType) { - UIHolderObjectBase currentHolder = leafScope.Holder; - while (currentHolder != null) + HotkeyScope current = leafScope; + while (current != null) { - if (_scopes.TryGetValue(currentHolder, out var scope) - && TryGetLatestRegistration(scope, actionId, pressType, out var registration)) + if (TryGetLatestRegistration(current, actionId, pressType, out var registration)) { registration.Trigger?.HotkeyActionTrigger(); return true; } - currentHolder = FindParentHolder(currentHolder); + UIHolderObjectBase parentHolder = current.ParentHolder; + current = parentHolder != null && _scopes.TryGetValue(parentHolder, out var parentScope) + ? parentScope + : null; } return false; @@ -470,7 +474,7 @@ namespace UnityEngine.UI return false; } - private static HotkeyScope[] GetLeafScopes() + private static HotkeyScope GetTopLeafScope() { _leafScopes.Clear(); _ancestorHolders.Clear(); @@ -486,11 +490,18 @@ namespace UnityEngine.UI continue; } - UIHolderObjectBase parentHolder = FindParentHolder(scope.Holder); + UIHolderObjectBase parentHolder = scope.ParentHolder; while (parentHolder != null) { _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); - return _leafScopes.ToArray(); + return _leafScopes[0]; } private static bool IsScopeActive(HotkeyScope scope)