优化+清理
This commit is contained in:
@@ -12,11 +12,14 @@ namespace GeometryTD.CustomComponent
|
||||
public class ResolutionAdapterComponent : GameFrameworkComponent
|
||||
{
|
||||
private const float DefaultCanvasPlaneDistance = 100f;
|
||||
private static ResolutionAdapterComponent s_Instance;
|
||||
|
||||
[SerializeField] private Vector2 _referenceResolution = new Vector2(2560f, 1600f);
|
||||
[SerializeField] private bool _adaptUiCanvasToViewport = true;
|
||||
[SerializeField] private bool _enableBlackMask = true;
|
||||
[SerializeField] private int _maskSortingOrder = short.MaxValue;
|
||||
[SerializeField] private bool _autoCollectSceneCanvases = true;
|
||||
[SerializeField] private float _canvasProbeInterval = 0.2f;
|
||||
[SerializeField] private List<Transform> _uiRoots = new List<Transform>();
|
||||
|
||||
private readonly List<Canvas> _canvasBuffer = new List<Canvas>(32);
|
||||
@@ -32,6 +35,7 @@ namespace GeometryTD.CustomComponent
|
||||
private bool _uiEventSubscribed;
|
||||
private bool _sceneEventSubscribed;
|
||||
private bool _missingRootWarned;
|
||||
private float _nextCanvasProbeTime;
|
||||
|
||||
private Canvas _maskCanvas;
|
||||
private RectTransform _leftMask;
|
||||
@@ -39,6 +43,23 @@ namespace GeometryTD.CustomComponent
|
||||
private RectTransform _topMask;
|
||||
private RectTransform _bottomMask;
|
||||
|
||||
public static bool TryGetTargetViewport(out Rect viewport)
|
||||
{
|
||||
if (s_Instance != null && s_Instance._cachedScreenWidth > 0 && s_Instance._cachedScreenHeight > 0)
|
||||
{
|
||||
viewport = s_Instance._targetViewport;
|
||||
return true;
|
||||
}
|
||||
|
||||
viewport = new Rect(0f, 0f, 1f, 1f);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
s_Instance = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
TryEnsureUiEventSubscribed();
|
||||
@@ -48,6 +69,10 @@ namespace GeometryTD.CustomComponent
|
||||
private void Update()
|
||||
{
|
||||
TryEnsureUiEventSubscribed();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
ApplyAdaptation(false);
|
||||
}
|
||||
|
||||
@@ -59,6 +84,11 @@ namespace GeometryTD.CustomComponent
|
||||
{
|
||||
_mainCamera.rect = new Rect(0f, 0f, 1f, 1f);
|
||||
}
|
||||
|
||||
if (s_Instance == this)
|
||||
{
|
||||
s_Instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyAdaptation(bool force)
|
||||
@@ -95,11 +125,7 @@ namespace GeometryTD.CustomComponent
|
||||
return;
|
||||
}
|
||||
|
||||
if (!needRecalculate && !_canvasCacheDirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TryMarkCanvasCacheDirtyByProbe();
|
||||
ApplyCanvasAdaptation();
|
||||
}
|
||||
|
||||
@@ -308,6 +334,7 @@ namespace GeometryTD.CustomComponent
|
||||
if (!_uiEventSubscribed)
|
||||
{
|
||||
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormChanged);
|
||||
GameEntry.Event.Subscribe(OpenUIFormUpdateEventArgs.EventId, OnUIFormChanged);
|
||||
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, OnUIFormChanged);
|
||||
_uiEventSubscribed = true;
|
||||
_canvasCacheDirty = true;
|
||||
@@ -331,6 +358,7 @@ namespace GeometryTD.CustomComponent
|
||||
if (_uiEventSubscribed)
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnUIFormChanged);
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormUpdateEventArgs.EventId, OnUIFormChanged);
|
||||
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, OnUIFormChanged);
|
||||
_uiEventSubscribed = false;
|
||||
}
|
||||
@@ -353,6 +381,47 @@ namespace GeometryTD.CustomComponent
|
||||
_missingRootWarned = false;
|
||||
}
|
||||
|
||||
private void TryMarkCanvasCacheDirtyByProbe()
|
||||
{
|
||||
if (_canvasCacheDirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_canvasProbeInterval > 0f && Time.unscaledTime < _nextCanvasProbeTime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_nextCanvasProbeTime = Time.unscaledTime + Mathf.Max(0.01f, _canvasProbeInterval);
|
||||
Canvas[] canvases = Resources.FindObjectsOfTypeAll<Canvas>();
|
||||
foreach (Canvas canvas in canvases)
|
||||
{
|
||||
if (canvas == null || canvas == _maskCanvas || canvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
GameObject gameObject = canvas.gameObject;
|
||||
if (!gameObject.scene.IsValid() || !gameObject.scene.isLoaded)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_trackedCanvasSet.Contains(canvas))
|
||||
{
|
||||
_canvasCacheDirty = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculateViewportPadding(out float horizontalPadding, out float verticalPadding)
|
||||
{
|
||||
horizontalPadding = Mathf.Max(0f, (1f - _targetViewport.width) * 0.5f * _cachedScreenWidth);
|
||||
verticalPadding = Mathf.Max(0f, (1f - _targetViewport.height) * 0.5f * _cachedScreenHeight);
|
||||
}
|
||||
|
||||
private void UpdateMaskLayout()
|
||||
{
|
||||
if (!_enableBlackMask)
|
||||
@@ -367,8 +436,13 @@ namespace GeometryTD.CustomComponent
|
||||
return;
|
||||
}
|
||||
|
||||
float horizontalPadding = Mathf.Max(0f, (1f - _targetViewport.width) * 0.5f * _cachedScreenWidth);
|
||||
float verticalPadding = Mathf.Max(0f, (1f - _targetViewport.height) * 0.5f * _cachedScreenHeight);
|
||||
int maskSortingOrder = Mathf.Clamp(_maskSortingOrder, short.MinValue, short.MaxValue);
|
||||
if (_maskCanvas.sortingOrder != maskSortingOrder)
|
||||
{
|
||||
_maskCanvas.sortingOrder = maskSortingOrder;
|
||||
}
|
||||
|
||||
CalculateViewportPadding(out float horizontalPadding, out float verticalPadding);
|
||||
|
||||
bool showVerticalMask = horizontalPadding > 0.5f;
|
||||
bool showHorizontalMask = verticalPadding > 0.5f;
|
||||
@@ -405,7 +479,7 @@ namespace GeometryTD.CustomComponent
|
||||
_maskCanvas = maskRoot.GetComponent<Canvas>();
|
||||
_maskCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
_maskCanvas.overrideSorting = true;
|
||||
_maskCanvas.sortingOrder = short.MaxValue;
|
||||
_maskCanvas.sortingOrder = Mathf.Clamp(_maskSortingOrder, short.MinValue, short.MaxValue);
|
||||
|
||||
CanvasScaler scaler = maskRoot.GetComponent<CanvasScaler>();
|
||||
scaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
|
||||
|
||||
Reference in New Issue
Block a user