优化性能

This commit is contained in:
陈思海 2026-04-09 15:21:49 +08:00
parent 0f7d062a9f
commit bc66728a65
3 changed files with 67 additions and 35 deletions

View File

@ -9,11 +9,13 @@ namespace UnityEngine.UI
internal sealed class UXNavigationRuntime : MonoBehaviour internal sealed class UXNavigationRuntime : MonoBehaviour
{ {
private static UXNavigationRuntime _instance; private static UXNavigationRuntime _instance;
private static readonly string CacheLayerName = $"Layer{(int)UILayer.All}-{UILayer.All}";
private const float DiscoveryInterval = 0.5f; 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();
private readonly List<UIHolderObjectBase> _holderBuffer = new(32);
private Transform _uiCanvasRoot; private Transform _uiCanvasRoot;
private UXNavigationScope _topScope; private UXNavigationScope _topScope;
@ -116,6 +118,7 @@ namespace UnityEngine.UI
_topScope = null; _topScope = null;
} }
scope.IsAvailable = false;
scope.SetNavigationSuppressed(false); scope.SetNavigationSuppressed(false);
_scopes.Remove(scope); _scopes.Remove(scope);
MarkDiscoveryDirty(); MarkDiscoveryDirty();
@ -185,11 +188,10 @@ namespace UnityEngine.UI
return; return;
} }
string cacheLayerName = $"Layer{(int)UILayer.All}-{UILayer.All}";
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);
if (child == null || child.name == cacheLayerName) if (child == null || child.name == CacheLayerName)
{ {
continue; continue;
} }
@ -217,10 +219,11 @@ namespace UnityEngine.UI
continue; continue;
} }
UIHolderObjectBase[] holders = layerRoot.GetComponentsInChildren<UIHolderObjectBase>(true); _holderBuffer.Clear();
for (int i = 0; i < holders.Length; i++) layerRoot.GetComponentsInChildren(true, _holderBuffer);
for (int i = 0; i < _holderBuffer.Count; i++)
{ {
UIHolderObjectBase holder = holders[i]; UIHolderObjectBase holder = _holderBuffer[i];
if (holder == null if (holder == null
|| holder.GetComponent<UXNavigationScope>() != null || holder.GetComponent<UXNavigationScope>() != null
|| IsNavigationSkipped(holder.transform)) || IsNavigationSkipped(holder.transform))
@ -233,6 +236,8 @@ namespace UnityEngine.UI
} }
} }
_holderBuffer.Clear();
if (addedScope) if (addedScope)
{ {
for (int i = 0; i < _scopes.Count; i++) for (int i = 0; i < _scopes.Count; i++)
@ -254,6 +259,7 @@ namespace UnityEngine.UI
} }
bool available = IsScopeAvailable(scope); bool available = IsScopeAvailable(scope);
scope.IsAvailable = available;
if (scope.WasAvailable != available) if (scope.WasAvailable != available)
{ {
scope.WasAvailable = available; scope.WasAvailable = available;
@ -348,7 +354,7 @@ namespace UnityEngine.UI
continue; continue;
} }
bool suppress = IsScopeAvailable(scope) bool suppress = scope.IsAvailable
&& _topScope != null && _topScope != null
&& !ReferenceEquals(scope, _topScope) && !ReferenceEquals(scope, _topScope)
&& _topScope.BlockLowerScopes && _topScope.BlockLowerScopes

View File

@ -21,16 +21,20 @@ namespace UnityEngine.UI
[SerializeField, Header("自动选中首个可用控件")] private bool _autoSelectFirstAvailable = true; [SerializeField, Header("自动选中首个可用控件")] private bool _autoSelectFirstAvailable = true;
private readonly List<Selectable> _cachedSelectables = new(16); private readonly List<Selectable> _cachedSelectables = new(16);
private readonly HashSet<Selectable> _cachedSelectableSet = new();
private readonly Dictionary<Selectable, Navigation> _baselineNavigation = new(); private readonly Dictionary<Selectable, Navigation> _baselineNavigation = new();
private readonly List<Selectable> _removeBuffer = new(8); private readonly List<Selectable> _removeBuffer = new(8);
private readonly List<Selectable> _selectableScanBuffer = new(16);
private Canvas _cachedCanvas; private Canvas _cachedCanvas;
private UIHolderObjectBase _cachedHolder; private UIHolderObjectBase _cachedHolder;
private Selectable _lastSelected; private Selectable _lastSelected;
private bool _cacheDirty = true; private bool _cacheDirty = true;
private bool _navigationSuppressed; private bool _navigationSuppressed;
private int _cachedHierarchyDepth = -1;
internal ulong ActivationSerial { get; set; } internal ulong ActivationSerial { get; set; }
internal bool IsAvailable { get; set; }
internal bool WasAvailable { get; set; } internal bool WasAvailable { get; set; }
public Selectable DefaultSelectable public Selectable DefaultSelectable
@ -98,6 +102,14 @@ namespace UnityEngine.UI
_cacheDirty = true; _cacheDirty = true;
} }
private void OnTransformParentChanged()
{
_cacheDirty = true;
_cachedCanvas = null;
_cachedHolder = null;
_cachedHierarchyDepth = -1;
}
#if UNITY_EDITOR #if UNITY_EDITOR
private void OnValidate() private void OnValidate()
{ {
@ -107,6 +119,11 @@ namespace UnityEngine.UI
internal int GetHierarchyDepth() internal int GetHierarchyDepth()
{ {
if (_cachedHierarchyDepth >= 0)
{
return _cachedHierarchyDepth;
}
int depth = 0; int depth = 0;
Transform current = transform; Transform current = transform;
while (current != null) while (current != null)
@ -115,7 +132,8 @@ namespace UnityEngine.UI
current = current.parent; current = current.parent;
} }
return depth; _cachedHierarchyDepth = depth;
return _cachedHierarchyDepth;
} }
internal bool Owns(GameObject target) internal bool Owns(GameObject target)
@ -151,7 +169,7 @@ namespace UnityEngine.UI
for (int i = 0; i < _cachedSelectables.Count; i++) for (int i = 0; i < _cachedSelectables.Count; i++)
{ {
Selectable selectable = _cachedSelectables[i]; Selectable selectable = _cachedSelectables[i];
if (IsSelectableValid(selectable)) if (IsSelectableUsable(selectable))
{ {
return selectable; return selectable;
} }
@ -171,7 +189,7 @@ namespace UnityEngine.UI
for (int i = 0; i < _cachedSelectables.Count; i++) for (int i = 0; i < _cachedSelectables.Count; i++)
{ {
if (IsSelectableValid(_cachedSelectables[i])) if (IsSelectableUsable(_cachedSelectables[i]))
{ {
return true; return true;
} }
@ -187,12 +205,7 @@ namespace UnityEngine.UI
return; return;
} }
Selectable selectable = selectedObject.GetComponent<Selectable>(); Selectable selectable = GetSelectableFromObject(selectedObject);
if (selectable == null)
{
selectable = selectedObject.GetComponentInParent<Selectable>();
}
if (IsSelectableValid(selectable)) if (IsSelectableValid(selectable))
{ {
_lastSelected = selectable; _lastSelected = selectable;
@ -201,18 +214,7 @@ namespace UnityEngine.UI
internal bool IsSelectableOwnedAndValid(GameObject selectedObject) internal bool IsSelectableOwnedAndValid(GameObject selectedObject)
{ {
if (selectedObject == null || !Owns(selectedObject)) return IsSelectableValid(GetSelectableFromObject(selectedObject));
{
return false;
}
Selectable selectable = selectedObject.GetComponent<Selectable>();
if (selectable == null)
{
selectable = selectedObject.GetComponentInParent<Selectable>();
}
return IsSelectableValid(selectable);
} }
internal void SetNavigationSuppressed(bool suppressed) internal void SetNavigationSuppressed(bool suppressed)
@ -259,6 +261,7 @@ namespace UnityEngine.UI
_cacheDirty = false; _cacheDirty = false;
_cachedSelectables.Clear(); _cachedSelectables.Clear();
_cachedSelectableSet.Clear();
if (_explicitSelectables != null && _explicitSelectables.Count > 0) if (_explicitSelectables != null && _explicitSelectables.Count > 0)
{ {
@ -269,17 +272,18 @@ namespace UnityEngine.UI
} }
else else
{ {
Selectable[] selectables = GetComponentsInChildren<Selectable>(true); _selectableScanBuffer.Clear();
for (int i = 0; i < selectables.Length; i++) GetComponentsInChildren(true, _selectableScanBuffer);
for (int i = 0; i < _selectableScanBuffer.Count; i++)
{ {
TryAddSelectable(selectables[i]); TryAddSelectable(_selectableScanBuffer[i]);
} }
} }
_removeBuffer.Clear(); _removeBuffer.Clear();
foreach (var pair in _baselineNavigation) foreach (var pair in _baselineNavigation)
{ {
if (!_cachedSelectables.Contains(pair.Key)) if (!_cachedSelectableSet.Contains(pair.Key))
{ {
_removeBuffer.Add(pair.Key); _removeBuffer.Add(pair.Key);
} }
@ -289,6 +293,8 @@ namespace UnityEngine.UI
{ {
_baselineNavigation.Remove(_removeBuffer[i]); _baselineNavigation.Remove(_removeBuffer[i]);
} }
_selectableScanBuffer.Clear();
} }
public void InvalidateSelectableCache() public void InvalidateSelectableCache()
@ -298,7 +304,7 @@ namespace UnityEngine.UI
private void TryAddSelectable(Selectable selectable) private void TryAddSelectable(Selectable selectable)
{ {
if (selectable == null || !Owns(selectable.gameObject)) if (selectable == null || !Owns(selectable.gameObject) || !_cachedSelectableSet.Add(selectable))
{ {
return; return;
} }
@ -311,11 +317,28 @@ namespace UnityEngine.UI
} }
private bool IsSelectableValid(Selectable selectable) private bool IsSelectableValid(Selectable selectable)
{
return IsSelectableUsable(selectable)
&& (_cachedSelectableSet.Contains(selectable) || Owns(selectable.gameObject));
}
private static bool IsSelectableUsable(Selectable selectable)
{ {
return selectable != null return selectable != null
&& selectable.IsActive() && selectable.IsActive()
&& selectable.IsInteractable() && selectable.IsInteractable();
&& Owns(selectable.gameObject); }
private static Selectable GetSelectableFromObject(GameObject selectedObject)
{
if (selectedObject == null)
{
return null;
}
return selectedObject.TryGetComponent(out Selectable selectable)
? selectable
: selectedObject.GetComponentInParent<Selectable>();
} }
} }
} }

View File

@ -17,6 +17,8 @@ namespace UnityEngine.UI
{ {
[SerializeField] private List<TransitionData> m_ChildTransitions = new(); [SerializeField] private List<TransitionData> m_ChildTransitions = new();
private SelectionState _state;
void StartChildColorTween(TransitionData transitionData, Color targetColor, bool instant) void StartChildColorTween(TransitionData transitionData, Color targetColor, bool instant)
{ {
if (transitionData.targetGraphic == null) if (transitionData.targetGraphic == null)
@ -68,7 +70,8 @@ namespace UnityEngine.UI
protected override void DoStateTransition(SelectionState state, bool instant) protected override void DoStateTransition(SelectionState state, bool instant)
{ {
base.DoStateTransition(state, instant); base.DoStateTransition(state, instant);
if (_state == state) return;
_state = state;
for (int i = 0; i < m_ChildTransitions.Count; i++) for (int i = 0; i < m_ChildTransitions.Count; i++)
{ {
TransitionData transitionData = m_ChildTransitions[i]; TransitionData transitionData = m_ChildTransitions[i];