Compare commits

..

No commits in common. "6f897a7af730cdd3a47e02496e69e8609b79b8ce" and "00a7eff5f84c992b08a52ff2c0a955e13e7a7587" have entirely different histories.

3 changed files with 23 additions and 87 deletions

View File

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

View File

@ -23,9 +23,6 @@ namespace UnityEngine.UI
private ulong _activationSerial; private ulong _activationSerial;
private bool _missingEventSystemLogged; private bool _missingEventSystemLogged;
private bool _topScopeDirty = true;
private bool _suppressionDirty = true;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
private static void Bootstrap() private static void Bootstrap()
{ {
@ -104,8 +101,6 @@ namespace UnityEngine.UI
} }
_scopes.Add(scope); _scopes.Add(scope);
_topScopeDirty = true;
_suppressionDirty = true;
} }
internal void UnregisterScope(UXNavigationScope scope) internal void UnregisterScope(UXNavigationScope scope)
@ -122,17 +117,7 @@ namespace UnityEngine.UI
scope.IsAvailable = false; scope.IsAvailable = false;
scope.SetNavigationSuppressed(false); scope.SetNavigationSuppressed(false);
_scopes.Remove(scope);
int idx = _scopes.IndexOf(scope);
if (idx >= 0)
{
int last = _scopes.Count - 1;
_scopes[idx] = _scopes[last];
_scopes.RemoveAt(last);
}
_topScopeDirty = true;
_suppressionDirty = true;
} }
internal void MarkDiscoveryDirty() internal void MarkDiscoveryDirty()
@ -153,22 +138,13 @@ namespace UnityEngine.UI
DiscoverScopes(); DiscoverScopes();
} }
if (_topScopeDirty) UXNavigationScope newTopScope = FindTopScope();
if (!ReferenceEquals(_topScope, newTopScope))
{ {
UXNavigationScope newTopScope = FindTopScope(); _topScope = newTopScope;
_topScopeDirty = false;
if (!ReferenceEquals(_topScope, newTopScope))
{
_topScope = newTopScope;
_suppressionDirty = true;
}
} }
if (_suppressionDirty) ApplyScopeSuppression();
{
ApplyScopeSuppression();
_suppressionDirty = false;
}
if (UXInputModeService.CurrentMode == UXInputMode.Gamepad) if (UXInputModeService.CurrentMode == UXInputMode.Gamepad)
{ {
@ -261,9 +237,6 @@ namespace UnityEngine.UI
{ {
_scopes[i]?.InvalidateSelectableCache(); _scopes[i]?.InvalidateSelectableCache();
} }
_topScopeDirty = true;
_suppressionDirty = true;
} }
} }
@ -302,8 +275,6 @@ namespace UnityEngine.UI
{ {
scope.ActivationSerial = ++_activationSerial; scope.ActivationSerial = ++_activationSerial;
} }
_suppressionDirty = true;
} }
if (!available) if (!available)
@ -333,13 +304,11 @@ namespace UnityEngine.UI
return false; return false;
} }
return !scope.IsNavigationSkipped return !IsNavigationSkipped(scope.transform)
&& scope.HasAvailableSelectable() && scope.HasAvailableSelectable()
&& TryGetInteractiveLayerRoot(scope.transform, out _); && TryGetInteractiveLayerRoot(scope.transform, out _);
} }
// 保留静态方法用于 DiscoverScopes 中对 LayerRoot / Holder 节点的检测
// 这些节点无 UXNavigationScope调用频次低仅在 dirty 时),无需缓存
private static bool IsNavigationSkipped(Transform current) private static bool IsNavigationSkipped(Transform current)
{ {
return current != null && current.GetComponentInParent<UXNavigationSkip>(true) != null; return current != null && current.GetComponentInParent<UXNavigationSkip>(true) != null;

View File

@ -33,9 +33,6 @@ namespace UnityEngine.UI
private bool _navigationSuppressed; private bool _navigationSuppressed;
private int _cachedHierarchyDepth = -1; private int _cachedHierarchyDepth = -1;
private bool _cachedIsSkipped;
private bool _isSkippedCacheValid;
internal ulong ActivationSerial { get; set; } internal ulong ActivationSerial { get; set; }
internal bool IsAvailable { get; set; } internal bool IsAvailable { get; set; }
internal bool WasAvailable { get; set; } internal bool WasAvailable { get; set; }
@ -50,19 +47,6 @@ namespace UnityEngine.UI
public bool RequireSelectionWhenGamepad => _requireSelectionWhenGamepad; public bool RequireSelectionWhenGamepad => _requireSelectionWhenGamepad;
public bool BlockLowerScopes => _blockLowerScopes; public bool BlockLowerScopes => _blockLowerScopes;
internal bool IsNavigationSkipped
{
get
{
if (!_isSkippedCacheValid)
{
_cachedIsSkipped = GetComponentInParent<UXNavigationSkip>(true) != null;
_isSkippedCacheValid = true;
}
return _cachedIsSkipped;
}
}
internal Canvas Canvas internal Canvas Canvas
{ {
get get
@ -124,7 +108,6 @@ namespace UnityEngine.UI
_cachedCanvas = null; _cachedCanvas = null;
_cachedHolder = null; _cachedHolder = null;
_cachedHierarchyDepth = -1; _cachedHierarchyDepth = -1;
_isSkippedCacheValid = false;
} }
#if UNITY_EDITOR #if UNITY_EDITOR
@ -236,12 +219,12 @@ namespace UnityEngine.UI
internal void SetNavigationSuppressed(bool suppressed) internal void SetNavigationSuppressed(bool suppressed)
{ {
RefreshSelectableCache();
if (_navigationSuppressed == suppressed) if (_navigationSuppressed == suppressed)
{ {
return; return;
} }
RefreshSelectableCache();
_navigationSuppressed = suppressed; _navigationSuppressed = suppressed;
for (int i = 0; i < _cachedSelectables.Count; i++) for (int i = 0; i < _cachedSelectables.Count; i++)
{ {
@ -298,11 +281,11 @@ namespace UnityEngine.UI
} }
_removeBuffer.Clear(); _removeBuffer.Clear();
foreach (Selectable key in _baselineNavigation.Keys) foreach (var pair in _baselineNavigation)
{ {
if (!_cachedSelectableSet.Contains(key)) if (!_cachedSelectableSet.Contains(pair.Key))
{ {
_removeBuffer.Add(key); _removeBuffer.Add(pair.Key);
} }
} }