From e639d83c2d8872999ba2d6ccdd38440cb00bd29a Mon Sep 17 00:00:00 2001 From: Ding Yuntian <1491671119@qq.com> Date: Fri, 13 Mar 2026 21:04:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E9=87=8D=E6=9E=84=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=AE=BD=E5=BA=A6=E8=AE=A1=E7=AE=97=E5=92=8C=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E5=B8=83=E5=B1=80=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/UI/DialogUI/Bubbles/Bubble.cs | 2 +- .../UI/DialogUI/Bubbles/BubbleGroup.cs | 11 ---- Assets/Scripts/UI/DialogUI/DialogUIManager.cs | 10 ++++ Assets/Scripts/UI/DialogUI/OnelineBox.cs | 56 +++++++++++++++++-- .../UI/DialogUI/Options/OnelineOption.cs | 13 +++-- .../UI/DialogUI/Options/OnelineOptions.cs | 4 +- Assets/Scripts/Utility/CommonUtil.cs | 10 +++- 7 files changed, 78 insertions(+), 28 deletions(-) diff --git a/Assets/Scripts/UI/DialogUI/Bubbles/Bubble.cs b/Assets/Scripts/UI/DialogUI/Bubbles/Bubble.cs index d631b154c..a19a40e58 100644 --- a/Assets/Scripts/UI/DialogUI/Bubbles/Bubble.cs +++ b/Assets/Scripts/UI/DialogUI/Bubbles/Bubble.cs @@ -162,7 +162,7 @@ namespace AibisDream } // 先去除富文本标签 - var width = _bubbleGroup.EstimateTextWidth(line.RemoveRichTextTags(), _lineText.fontSize); + var width = DialogUIManager.Instance.EstimateTextWidth(line.RemoveRichTextTags(), _lineText.fontSize); if (width > maxWidth) { _layoutElement.preferredWidth = maxWidth; diff --git a/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs b/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs index 69c707dcf..bac64cf79 100644 --- a/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs +++ b/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs @@ -6,15 +6,12 @@ using System.Threading.Tasks; using AibisDream.Framework; using AibisDream.Kit; using AibisDream.Utility; -using TMPro; using UnityEngine; namespace AibisDream { public class BubbleGroup : MonoBehaviour, ILineView { - [SerializeField] private TMP_Text widthEstimation; - private Dictionary _bubbleDict = new(); private BubbleFactory _bubbleFactory; private CancellationTokenSource _autoHideToken; @@ -89,14 +86,6 @@ namespace AibisDream } } - public float EstimateTextWidth(string text, float fontSize) - { - widthEstimation.fontSize = fontSize; - widthEstimation.text = text; - widthEstimation.ForceMeshUpdate(); - return widthEstimation.preferredWidth; - } - private async void AfterAdvance() { _autoHideToken = new CancellationTokenSource(); diff --git a/Assets/Scripts/UI/DialogUI/DialogUIManager.cs b/Assets/Scripts/UI/DialogUI/DialogUIManager.cs index ef706200c..f314ec494 100644 --- a/Assets/Scripts/UI/DialogUI/DialogUIManager.cs +++ b/Assets/Scripts/UI/DialogUI/DialogUIManager.cs @@ -1,6 +1,7 @@ using AibisDream.Framework; using AibisDream.Kit; using AibisDream.UI; +using TMPro; using UnityEngine; namespace AibisDream @@ -9,6 +10,7 @@ namespace AibisDream { [SerializeField] private LineRunner lineView; [SerializeField] private OptionView optionView; + [SerializeField] private TMP_Text widthEstimation; public override void OnSingletonInit() { @@ -77,6 +79,14 @@ namespace AibisDream lineView.LoadBubbleData(data); optionView.LoadBubbles(data); } + + public float EstimateTextWidth(string text, float fontSize) + { + widthEstimation.fontSize = fontSize; + widthEstimation.text = text; + widthEstimation.ForceMeshUpdate(); + return widthEstimation.preferredWidth; + } } public enum DialogViewType diff --git a/Assets/Scripts/UI/DialogUI/OnelineBox.cs b/Assets/Scripts/UI/DialogUI/OnelineBox.cs index dfb4377ff..a85cbd0a1 100644 --- a/Assets/Scripts/UI/DialogUI/OnelineBox.cs +++ b/Assets/Scripts/UI/DialogUI/OnelineBox.cs @@ -1,8 +1,10 @@ -using System; +using System; using System.Threading; using System.Threading.Tasks; using Febucci.UI.Core; +using TMPro; using UnityEngine; +using UnityEngine.UI; using AibisDream.Framework; using AibisDream.Utility; @@ -13,14 +15,21 @@ namespace AibisDream [SerializeField] private TypewriterCore content; [SerializeField] private GameObject box; + [Header("排版参数")] + [SerializeField] private int maxWidth = 800; + [SerializeField] private int minWidth = 10; + + private LayoutElement _contentLayoutElement; + private TMP_Text _contentText; + private RectTransform _rectTransform; private CancellationTokenSource _autoHideToken; private event Action OnTextShowed; + private bool _isInitialized; // ---------------------------- 显示逻辑 ---------------------------- public void RunLine(LineSyncToken syncToken) { - Debug.Log("OnelineBox: " + syncToken.lineInfo.lineText); // 先中断结束事件 _autoHideToken?.Cancel(); _autoHideToken = null; @@ -32,11 +41,11 @@ namespace AibisDream // 播放文字 content.TextAnimator.SetText(string.Empty); - Debug.Log("OnelineBox: " + GetOneLineText(syncToken.lineInfo)); + var lineText = GetOneLineText(syncToken.lineInfo); box.SetActive(true); - content.ShowText(GetOneLineText(syncToken.lineInfo)); + HandleLayout(lineText); + content.ShowText(lineText); syncToken.TextStart(); - Debug.Log("OnelineBox: TextStart"); } private string GetOneLineText(LineInfo lineInfo) @@ -45,10 +54,34 @@ namespace AibisDream { return lineInfo.lineText; } - + return $"{lineInfo.character.GetActorName()}: {lineInfo.lineText}"; } + private void HandleLayout(string line) + { + if (!_isInitialized) + { + InitRefs(); + } + + // 去除富文本标签后计算宽度 + var width = DialogUIManager.Instance.EstimateTextWidth(line.RemoveRichTextTags(), _contentText.fontSize); + + if (width > maxWidth) + { + _contentLayoutElement.preferredWidth = maxWidth; + } + else + { + _contentLayoutElement.preferredWidth = -1; // 自适应 + } + + _contentLayoutElement.minWidth = minWidth; + + LayoutRebuilder.ForceRebuildLayoutImmediate(_rectTransform); + } + private async void AfterAdvance() { _autoHideToken = new CancellationTokenSource(); @@ -73,6 +106,17 @@ namespace AibisDream private void Awake() { box.SetActive(false); + InitRefs(); + } + + private void InitRefs() + { + if (_isInitialized) return; + + _rectTransform = box.GetComponent(); + _contentText = content.GetComponent(); + _contentLayoutElement = content.GetComponent(); + _isInitialized = true; } private void Start() diff --git a/Assets/Scripts/UI/DialogUI/Options/OnelineOption.cs b/Assets/Scripts/UI/DialogUI/Options/OnelineOption.cs index 161214c3c..8b21df058 100644 --- a/Assets/Scripts/UI/DialogUI/Options/OnelineOption.cs +++ b/Assets/Scripts/UI/DialogUI/Options/OnelineOption.cs @@ -1,7 +1,8 @@ -using System; +using System; using UnityEngine; using UnityEngine.UI; using TMPro; +using AibisDream.Utility; namespace AibisDream { @@ -49,23 +50,23 @@ namespace AibisDream switch (state) { case SelectionType.Normal: - arrow.gameObject.SetActive(false); + arrow.SetAlpha(0f); SetColor(colorBlock.normalColor); break; case SelectionType.Highlighted: - arrow.gameObject.SetActive(true); + arrow.SetAlpha(1f); SetColor(colorBlock.highlightedColor); break; case SelectionType.Pressed: - arrow.gameObject.SetActive(true); + arrow.SetAlpha(1f); SetColor(colorBlock.pressedColor); break; case SelectionType.Selected: - arrow.gameObject.SetActive(true); + arrow.SetAlpha(1f); SetColor(colorBlock.selectedColor); break; case SelectionType.Disabled: - arrow.gameObject.SetActive(false); + arrow.SetAlpha(0f); SetColor(colorBlock.disabledColor); break; } diff --git a/Assets/Scripts/UI/DialogUI/Options/OnelineOptions.cs b/Assets/Scripts/UI/DialogUI/Options/OnelineOptions.cs index 10dc8e88f..b71c6e470 100644 --- a/Assets/Scripts/UI/DialogUI/Options/OnelineOptions.cs +++ b/Assets/Scripts/UI/DialogUI/Options/OnelineOptions.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using AibisDream.Framework; namespace AibisDream @@ -12,8 +12,6 @@ namespace AibisDream { gameObject.SetActive(true); - Debug.Log($"ShowOptions: {dialogOptions.Length}, {dialogOptions[0].Line}"); - // 生成新按钮 foreach (var option in dialogOptions) { diff --git a/Assets/Scripts/Utility/CommonUtil.cs b/Assets/Scripts/Utility/CommonUtil.cs index 1bdc69c5e..0b39a2564 100644 --- a/Assets/Scripts/Utility/CommonUtil.cs +++ b/Assets/Scripts/Utility/CommonUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Linq; @@ -10,6 +10,7 @@ using UnityEngine; using Object = UnityEngine.Object; using System.Globalization; using UnityEditor; +using UnityEngine.UI; namespace AibisDream.Utility { @@ -185,6 +186,13 @@ namespace AibisDream.Utility renderer.color = color; } + public static void SetAlpha(this Image image, float targetAlpha) + { + Color color = image.color; + color.a = targetAlpha; + image.color = color; + } + public static void DestroyAllChildren(this Transform self) { if (self == null)