优化+清理
This commit is contained in:
@@ -8,18 +8,24 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
using GeometryTD.CustomComponent;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
/// <summary>
|
||||
/// uGUI 界面组辅助器。
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(200)]
|
||||
public class UGuiGroupHelper : UIGroupHelperBase
|
||||
{
|
||||
public const int DepthFactor = 10000;
|
||||
|
||||
private int m_Depth = 0;
|
||||
private Canvas m_CachedCanvas = null;
|
||||
private RectTransform m_RectTransform = null;
|
||||
private int m_LastScreenWidth = -1;
|
||||
private int m_LastScreenHeight = -1;
|
||||
private Rect m_LastViewport = new Rect(float.MinValue, float.MinValue, float.MinValue, float.MinValue);
|
||||
|
||||
/// <summary>
|
||||
/// 设置界面组深度。
|
||||
@@ -36,6 +42,7 @@ namespace UI
|
||||
{
|
||||
m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
|
||||
gameObject.GetOrAddComponent<GraphicRaycaster>();
|
||||
m_RectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -43,11 +50,94 @@ namespace UI
|
||||
m_CachedCanvas.overrideSorting = true;
|
||||
m_CachedCanvas.sortingOrder = DepthFactor * m_Depth;
|
||||
|
||||
RectTransform transform = GetComponent<RectTransform>();
|
||||
transform.anchorMin = Vector2.zero;
|
||||
transform.anchorMax = Vector2.one;
|
||||
transform.anchoredPosition = Vector2.zero;
|
||||
transform.sizeDelta = Vector2.zero;
|
||||
if (m_RectTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_RectTransform.anchorMin = Vector2.zero;
|
||||
m_RectTransform.anchorMax = Vector2.one;
|
||||
m_RectTransform.pivot = new Vector2(0.5f, 0.5f);
|
||||
m_RectTransform.anchoredPosition = Vector2.zero;
|
||||
m_RectTransform.sizeDelta = Vector2.zero;
|
||||
ApplyViewportClip(true);
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
ApplyViewportClip(false);
|
||||
}
|
||||
|
||||
private void ApplyViewportClip(bool force)
|
||||
{
|
||||
if (m_RectTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int screenWidth = Screen.width;
|
||||
int screenHeight = Screen.height;
|
||||
Rect viewport = ResolveViewportRect();
|
||||
if (!force
|
||||
&& screenWidth == m_LastScreenWidth
|
||||
&& screenHeight == m_LastScreenHeight
|
||||
&& Mathf.Approximately(viewport.x, m_LastViewport.x)
|
||||
&& Mathf.Approximately(viewport.y, m_LastViewport.y)
|
||||
&& Mathf.Approximately(viewport.width, m_LastViewport.width)
|
||||
&& Mathf.Approximately(viewport.height, m_LastViewport.height))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_LastScreenWidth = screenWidth;
|
||||
m_LastScreenHeight = screenHeight;
|
||||
m_LastViewport = viewport;
|
||||
|
||||
if (screenWidth <= 0 || screenHeight <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float horizontalPadding = Mathf.Max(0f, (1f - viewport.width) * 0.5f * screenWidth);
|
||||
float verticalPadding = Mathf.Max(0f, (1f - viewport.height) * 0.5f * screenHeight);
|
||||
float canvasScale = ResolveCanvasScaleFactor();
|
||||
float horizontalPaddingInCanvasUnit = horizontalPadding / canvasScale;
|
||||
float verticalPaddingInCanvasUnit = verticalPadding / canvasScale;
|
||||
m_RectTransform.offsetMin = new Vector2(horizontalPaddingInCanvasUnit, verticalPaddingInCanvasUnit);
|
||||
m_RectTransform.offsetMax = new Vector2(-horizontalPaddingInCanvasUnit, -verticalPaddingInCanvasUnit);
|
||||
}
|
||||
|
||||
private Rect ResolveViewportRect()
|
||||
{
|
||||
if (ResolutionAdapterComponent.TryGetTargetViewport(out Rect adapterViewport))
|
||||
{
|
||||
return adapterViewport;
|
||||
}
|
||||
|
||||
Camera camera = m_CachedCanvas != null ? m_CachedCanvas.worldCamera : null;
|
||||
if (camera == null && m_CachedCanvas != null && m_CachedCanvas.rootCanvas != null)
|
||||
{
|
||||
camera = m_CachedCanvas.rootCanvas.worldCamera;
|
||||
}
|
||||
|
||||
if (camera == null && GameEntry.Scene != null)
|
||||
{
|
||||
camera = GameEntry.Scene.MainCamera;
|
||||
}
|
||||
|
||||
if (camera == null)
|
||||
{
|
||||
camera = Camera.main;
|
||||
}
|
||||
|
||||
return camera != null ? camera.rect : new Rect(0f, 0f, 1f, 1f);
|
||||
}
|
||||
|
||||
private float ResolveCanvasScaleFactor()
|
||||
{
|
||||
Canvas rootCanvas = m_CachedCanvas != null ? m_CachedCanvas.rootCanvas : null;
|
||||
float scale = rootCanvas != null ? rootCanvas.scaleFactor : (m_CachedCanvas != null ? m_CachedCanvas.scaleFactor : 1f);
|
||||
return scale > 0.0001f ? scale : 1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,9 +49,7 @@ namespace UI
|
||||
{
|
||||
"Other",
|
||||
"Subtitle",
|
||||
"Time",
|
||||
"LiangSiCheng",
|
||||
"LinHuiYin"
|
||||
"Time"
|
||||
};
|
||||
|
||||
public override void StartDialog(DialogFormContext context)
|
||||
@@ -130,6 +128,13 @@ namespace UI
|
||||
return;
|
||||
}
|
||||
|
||||
if (!HasSpeakerPortrait(_context.SpeakerId))
|
||||
{
|
||||
ClearSpeakerState();
|
||||
ApplySpeakerLayout(false, false, true);
|
||||
return;
|
||||
}
|
||||
|
||||
bool isRightSpeaker = context.Direction > 0;
|
||||
if (isRightSpeaker)
|
||||
{
|
||||
@@ -381,13 +386,21 @@ namespace UI
|
||||
int requestVersion = isRightSpeaker ? ++_rightPortraitRequestVersion : ++_leftPortraitRequestVersion;
|
||||
if (string.IsNullOrWhiteSpace(speakerId) || NonPortraitSpeakerIds.Contains(speakerId))
|
||||
{
|
||||
portraitImage.sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
int portraitCount = ResolveAvailablePortraitCount(speakerId);
|
||||
if (portraitCount <= 0)
|
||||
{
|
||||
portraitImage.sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
string portraitAssetName = $"Characters/{speakerId}-{ResolvePortraitIndex(speakerId, lineId, portraitCount)}.png";
|
||||
if (GameEntry.SpriteCache == null)
|
||||
{
|
||||
portraitImage.sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -400,10 +413,7 @@ namespace UI
|
||||
return;
|
||||
}
|
||||
|
||||
if (sprite != null)
|
||||
{
|
||||
_rightImage.sprite = sprite;
|
||||
}
|
||||
_rightImage.sprite = sprite;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -412,10 +422,7 @@ namespace UI
|
||||
return;
|
||||
}
|
||||
|
||||
if (sprite != null)
|
||||
{
|
||||
_leftImage.sprite = sprite;
|
||||
}
|
||||
_leftImage.sprite = sprite;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -449,6 +456,11 @@ namespace UI
|
||||
return GameEntry.Dialog.GetSpeakerPortraitCount(speakerId);
|
||||
}
|
||||
|
||||
private static bool HasSpeakerPortrait(string speakerId)
|
||||
{
|
||||
return ResolveAvailablePortraitCount(speakerId) > 0;
|
||||
}
|
||||
|
||||
private static bool IsHumanSpeaker(string speakerId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(speakerId))
|
||||
|
||||
@@ -46,4 +46,4 @@ namespace UI
|
||||
GameEntry.Event.Fire(this, MenuExitEventArgs.Create());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ namespace UI
|
||||
{
|
||||
public class SettingForm : UGuiForm
|
||||
{
|
||||
private static readonly string[] NavigateButtonNames = { "SoundButton", "GameButton", "ScreenButton" };
|
||||
|
||||
[SerializeField] private Transform _navigate;
|
||||
|
||||
[SerializeField] private Image[] _navigateButtonImages;
|
||||
@@ -95,20 +97,58 @@ namespace UI
|
||||
|
||||
public void OnNavigateButtonClick(int index)
|
||||
{
|
||||
RefreshNavigateButtonImages();
|
||||
|
||||
if (_navigateButtonImages == null || _navigateButtonImages.Length == 0)
|
||||
{
|
||||
Log.Error("Navigate button images are invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (index < 0 || index >= _navigateButtonImages.Length)
|
||||
{
|
||||
Log.Error("NavigateButtonClick index is out of range.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_navigateButtonImages[0] == null)
|
||||
_navigateButtonImages[0] = _navigate.Find("SoundButton")?.GetComponent<Image>();
|
||||
|
||||
for (int i = 0; i < _navigateButtonImages.Length; i++)
|
||||
{
|
||||
if (_navigateButtonImages[i] == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_navigateButtonImages[i].enabled = i == index;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshNavigateButtonImages()
|
||||
{
|
||||
if (_navigate == null)
|
||||
{
|
||||
Log.Error("Navigate transform is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_navigateButtonImages == null || _navigateButtonImages.Length != NavigateButtonNames.Length)
|
||||
{
|
||||
_navigateButtonImages = new Image[NavigateButtonNames.Length];
|
||||
}
|
||||
|
||||
for (int i = 0; i < NavigateButtonNames.Length; i++)
|
||||
{
|
||||
var button = _navigate.Find(NavigateButtonNames[i]);
|
||||
if (button == null)
|
||||
{
|
||||
Log.Error($"Can not find navigate button '{NavigateButtonNames[i]}'.");
|
||||
_navigateButtonImages[i] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
_navigateButtonImages[i] = button.GetComponent<Image>();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReturnButtonClick()
|
||||
{
|
||||
var dialogParams = new DialogParams
|
||||
|
||||
Reference in New Issue
Block a user