This commit is contained in:
陈思海 2026-04-09 15:28:20 +08:00
parent bc66728a65
commit 6bf99f313c

View File

@ -11,7 +11,6 @@ namespace UnityEngine.UI
private static UXNavigationRuntime _instance; private static UXNavigationRuntime _instance;
private static readonly string CacheLayerName = $"Layer{(int)UILayer.All}-{UILayer.All}"; private static readonly string CacheLayerName = $"Layer{(int)UILayer.All}-{UILayer.All}";
private const float DiscoveryInterval = 0.5f;
private readonly HashSet<UXNavigationScope> _scopeSet = new(); private readonly HashSet<UXNavigationScope> _scopeSet = new();
private readonly List<UXNavigationScope> _scopes = new(32); private readonly List<UXNavigationScope> _scopes = new(32);
private readonly HashSet<Transform> _interactiveLayerRoots = new(); private readonly HashSet<Transform> _interactiveLayerRoots = new();
@ -20,7 +19,6 @@ namespace UnityEngine.UI
private Transform _uiCanvasRoot; private Transform _uiCanvasRoot;
private UXNavigationScope _topScope; private UXNavigationScope _topScope;
private GameObject _lastObservedSelection; private GameObject _lastObservedSelection;
private float _nextDiscoveryTime;
private bool _discoveryDirty = true; private bool _discoveryDirty = true;
private ulong _activationSerial; private ulong _activationSerial;
private bool _missingEventSystemLogged; private bool _missingEventSystemLogged;
@ -103,7 +101,6 @@ namespace UnityEngine.UI
} }
_scopes.Add(scope); _scopes.Add(scope);
MarkDiscoveryDirty();
} }
internal void UnregisterScope(UXNavigationScope scope) internal void UnregisterScope(UXNavigationScope scope)
@ -121,7 +118,6 @@ namespace UnityEngine.UI
scope.IsAvailable = false; scope.IsAvailable = false;
scope.SetNavigationSuppressed(false); scope.SetNavigationSuppressed(false);
_scopes.Remove(scope); _scopes.Remove(scope);
MarkDiscoveryDirty();
} }
internal void MarkDiscoveryDirty() internal void MarkDiscoveryDirty()
@ -137,7 +133,7 @@ namespace UnityEngine.UI
return; return;
} }
if (_discoveryDirty || Time.unscaledTime >= _nextDiscoveryTime) if (_discoveryDirty)
{ {
DiscoverScopes(); DiscoverScopes();
} }
@ -176,6 +172,7 @@ namespace UnityEngine.UI
} }
_uiCanvasRoot = uiModule.UICanvasRoot; _uiCanvasRoot = uiModule.UICanvasRoot;
EnsureWatcher(_uiCanvasRoot);
CacheInteractiveLayers(); CacheInteractiveLayers();
DiscoverScopes(); DiscoverScopes();
} }
@ -188,6 +185,7 @@ namespace UnityEngine.UI
return; return;
} }
EnsureWatcher(_uiCanvasRoot);
for (int i = 0; i < _uiCanvasRoot.childCount; i++) for (int i = 0; i < _uiCanvasRoot.childCount; i++)
{ {
Transform child = _uiCanvasRoot.GetChild(i); Transform child = _uiCanvasRoot.GetChild(i);
@ -197,18 +195,13 @@ namespace UnityEngine.UI
} }
_interactiveLayerRoots.Add(child); _interactiveLayerRoots.Add(child);
if (child.GetComponent<UXNavigationLayerWatcher>() == null) EnsureWatcher(child);
{
var watcher = child.gameObject.AddComponent<UXNavigationLayerWatcher>();
watcher.Initialize(this);
}
} }
} }
private void DiscoverScopes() private void DiscoverScopes()
{ {
_discoveryDirty = false; _discoveryDirty = false;
_nextDiscoveryTime = Time.unscaledTime + DiscoveryInterval;
CacheInteractiveLayers(); CacheInteractiveLayers();
bool addedScope = false; bool addedScope = false;
@ -247,6 +240,21 @@ namespace UnityEngine.UI
} }
} }
private void EnsureWatcher(Transform target)
{
if (target == null)
{
return;
}
if (!target.TryGetComponent(out UXNavigationLayerWatcher watcher))
{
watcher = target.gameObject.AddComponent<UXNavigationLayerWatcher>();
}
watcher.Initialize(this);
}
private UXNavigationScope FindTopScope() private UXNavigationScope FindTopScope()
{ {
UXNavigationScope bestScope = null; UXNavigationScope bestScope = null;