From 6bf99f313cef580a226381b86542d1e4d5d9fef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Thu, 9 Apr 2026 15:28:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Navigation/UXNavigationRuntime.cs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Runtime/UXComponent/Navigation/UXNavigationRuntime.cs b/Runtime/UXComponent/Navigation/UXNavigationRuntime.cs index a9374be..031c5a8 100644 --- a/Runtime/UXComponent/Navigation/UXNavigationRuntime.cs +++ b/Runtime/UXComponent/Navigation/UXNavigationRuntime.cs @@ -11,7 +11,6 @@ namespace UnityEngine.UI private static UXNavigationRuntime _instance; private static readonly string CacheLayerName = $"Layer{(int)UILayer.All}-{UILayer.All}"; - private const float DiscoveryInterval = 0.5f; private readonly HashSet _scopeSet = new(); private readonly List _scopes = new(32); private readonly HashSet _interactiveLayerRoots = new(); @@ -20,7 +19,6 @@ namespace UnityEngine.UI private Transform _uiCanvasRoot; private UXNavigationScope _topScope; private GameObject _lastObservedSelection; - private float _nextDiscoveryTime; private bool _discoveryDirty = true; private ulong _activationSerial; private bool _missingEventSystemLogged; @@ -103,7 +101,6 @@ namespace UnityEngine.UI } _scopes.Add(scope); - MarkDiscoveryDirty(); } internal void UnregisterScope(UXNavigationScope scope) @@ -121,7 +118,6 @@ namespace UnityEngine.UI scope.IsAvailable = false; scope.SetNavigationSuppressed(false); _scopes.Remove(scope); - MarkDiscoveryDirty(); } internal void MarkDiscoveryDirty() @@ -137,7 +133,7 @@ namespace UnityEngine.UI return; } - if (_discoveryDirty || Time.unscaledTime >= _nextDiscoveryTime) + if (_discoveryDirty) { DiscoverScopes(); } @@ -176,6 +172,7 @@ namespace UnityEngine.UI } _uiCanvasRoot = uiModule.UICanvasRoot; + EnsureWatcher(_uiCanvasRoot); CacheInteractiveLayers(); DiscoverScopes(); } @@ -188,6 +185,7 @@ namespace UnityEngine.UI return; } + EnsureWatcher(_uiCanvasRoot); for (int i = 0; i < _uiCanvasRoot.childCount; i++) { Transform child = _uiCanvasRoot.GetChild(i); @@ -197,18 +195,13 @@ namespace UnityEngine.UI } _interactiveLayerRoots.Add(child); - if (child.GetComponent() == null) - { - var watcher = child.gameObject.AddComponent(); - watcher.Initialize(this); - } + EnsureWatcher(child); } } private void DiscoverScopes() { _discoveryDirty = false; - _nextDiscoveryTime = Time.unscaledTime + DiscoveryInterval; CacheInteractiveLayers(); 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(); + } + + watcher.Initialize(this); + } + private UXNavigationScope FindTopScope() { UXNavigationScope bestScope = null;