修改
This commit is contained in:
@@ -1,35 +1,32 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Kit;
|
||||
using DG.Tweening;
|
||||
using AibisDream.Utility;
|
||||
using Febucci.UI;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace AibisDream.FixSystem
|
||||
{
|
||||
public class ScreenSystem : MonoBehaviour, IScreen, IDialogView
|
||||
public class ScreenSystem : MonoBehaviour, IDialogView
|
||||
{
|
||||
#region 效果参数
|
||||
|
||||
public float typeSpeed = 0.05f;
|
||||
public int maxTextLine;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 索引
|
||||
|
||||
[SerializeField] private TMP_Text title;
|
||||
[SerializeField] private TMP_Text text;
|
||||
[SerializeField] private RectTransform textRoot;
|
||||
[SerializeField] private TMP_Text textForEstimation;
|
||||
private Action _nextStep;
|
||||
|
||||
private TextGenerator _generator;
|
||||
private TextGenerationSettings _generationSettings;
|
||||
|
||||
private IUnRegister _plugOutRmv;
|
||||
private GameObject _textItemPrefab;
|
||||
private Queue<TypewriterByCharacter> _lineQueue;
|
||||
|
||||
public TaskScreen TaskScreen { get; private set; }
|
||||
|
||||
public int maxLineNum = 10;
|
||||
|
||||
#endregion
|
||||
|
||||
private void Awake()
|
||||
@@ -40,19 +37,10 @@ namespace AibisDream.FixSystem
|
||||
|
||||
private void InitComponentRefs()
|
||||
{
|
||||
_generator = new TextGenerator();
|
||||
_generationSettings = new TextGenerationSettings
|
||||
{
|
||||
font = text.font.sourceFontFile,
|
||||
fontSize = Mathf.RoundToInt(text.fontSize),
|
||||
lineSpacing = text.lineSpacing,
|
||||
horizontalOverflow = HorizontalWrapMode.Wrap,
|
||||
verticalOverflow = VerticalWrapMode.Truncate,
|
||||
generationExtents = new Vector2(text.rectTransform.rect.width, Mathf.Infinity),
|
||||
resizeTextForBestFit = false
|
||||
};
|
||||
|
||||
TaskScreen = GetComponentInChildren<TaskScreen>();
|
||||
_lineQueue = new Queue<TypewriterByCharacter>();
|
||||
_textItemPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.ScreenTextName);
|
||||
// TODO 调整估算文本框的宽度
|
||||
}
|
||||
|
||||
private void Register()
|
||||
@@ -71,73 +59,26 @@ namespace AibisDream.FixSystem
|
||||
|
||||
#region 基础功能
|
||||
|
||||
public void ShowTitle(string targetTitle)
|
||||
private void ShowTitle(string targetTitle)
|
||||
{
|
||||
title.text = targetTitle;
|
||||
}
|
||||
|
||||
public IEnumerator ShowText(string targetText)
|
||||
{
|
||||
// 先换行
|
||||
if (!string.IsNullOrEmpty(text.text))
|
||||
{
|
||||
text.text += "\n";
|
||||
}
|
||||
// 先估算新增行数
|
||||
var targetLineNum = EstimateLineNum(targetText);
|
||||
// 截断超出屏幕的部分
|
||||
var oldText = "";
|
||||
if (targetLineNum + text.textInfo.lineCount > maxTextLine)
|
||||
{
|
||||
var cutLineNum = targetLineNum + text.textInfo.lineCount - maxTextLine;
|
||||
if (cutLineNum <= text.textInfo.lineCount)
|
||||
{
|
||||
var lastCharIndex = text.textInfo.lineInfo[cutLineNum - 1].lastCharacterIndex;
|
||||
if (lastCharIndex + 1 < text.text.Length)
|
||||
{
|
||||
oldText = text.text.Substring(lastCharIndex + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
oldText = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
oldText = text.text;
|
||||
}
|
||||
|
||||
|
||||
var targetLength = oldText.Length + targetText.Length;
|
||||
|
||||
// 逐个字符添加
|
||||
yield return DOTween.To(() => oldText.Length, x => UpdateText(x, targetText, oldText), targetLength,
|
||||
targetText.Length * typeSpeed).WaitForCompletion();
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
|
||||
_nextStep.Invoke();
|
||||
}
|
||||
|
||||
private int EstimateLineNum(string str)
|
||||
{
|
||||
var height = _generator.GetPreferredHeight(str, _generationSettings);
|
||||
var lineHeight = text.fontSize * 1.2f; // 简单估算行高
|
||||
|
||||
return Mathf.CeilToInt(height / lineHeight);
|
||||
}
|
||||
|
||||
private void UpdateText(int currentLength, string nextText, string oldText)
|
||||
{
|
||||
var appendLength = currentLength - oldText.Length;
|
||||
text.text = oldText + nextText[..appendLength];
|
||||
}
|
||||
|
||||
public void ClearTextScreen()
|
||||
{
|
||||
title.text = "";
|
||||
text.SetText("");
|
||||
while (_lineQueue.Count > 0)
|
||||
{
|
||||
var text = _lineQueue.Dequeue();
|
||||
Destroy(text.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -147,9 +88,31 @@ namespace AibisDream.FixSystem
|
||||
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId, bool isAutoSkip = false)
|
||||
{
|
||||
ShowTitle(character.GetActorName());
|
||||
StartCoroutine(ShowText(dialogLine));
|
||||
// 生成新文本组件
|
||||
var textItem = Instantiate(_textItemPrefab, textRoot);
|
||||
var textItemTypewriter = textItem.GetComponent<TypewriterByCharacter>();
|
||||
// 估算文本高度
|
||||
var newLineNum = EstimateLineNum(dialogLine);
|
||||
var oldLineNum = CalcTotalLineNum();
|
||||
if (newLineNum + oldLineNum > maxLineNum)
|
||||
{
|
||||
Destroy(_lineQueue.Dequeue().gameObject);
|
||||
}
|
||||
_lineQueue.Enqueue(textItemTypewriter);
|
||||
|
||||
// 播放文本
|
||||
textItemTypewriter.ShowText(dialogLine);
|
||||
// 播放完成后,顿0.3秒执行下一步
|
||||
textItemTypewriter.onTextShowed.AddListener(AutoNextStep);
|
||||
// 每行自动跳过
|
||||
_nextStep = nextStep;
|
||||
// 强制更新布局
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(textRoot);
|
||||
}
|
||||
|
||||
private void AutoNextStep()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void ShowOptions(DialogOption[] dialogueOptions)
|
||||
@@ -179,20 +142,17 @@ namespace AibisDream.FixSystem
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
private int EstimateLineNum(string text)
|
||||
{
|
||||
textForEstimation.text = text;
|
||||
textForEstimation.ForceMeshUpdate();
|
||||
return textForEstimation.textInfo.linkCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 屏幕接口
|
||||
/// </summary>
|
||||
public interface IScreen
|
||||
{
|
||||
// 显示标题文字
|
||||
void ShowTitle(string title);
|
||||
|
||||
// 显示正文
|
||||
IEnumerator ShowText(string text);
|
||||
|
||||
// 清空屏幕
|
||||
void ClearTextScreen();
|
||||
private int CalcTotalLineNum()
|
||||
{
|
||||
return _lineQueue.Select(x => x.GetComponent<TMP_Text>().textInfo.linkCount).Sum();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user