Files
aibis-dream/Assets/Scripts/UI/DialogUI/CenterText.cs
T
2025-05-16 21:47:35 +08:00

125 lines
3.3 KiB
C#

using System;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using Febucci.UI;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AibisDream
{
public class CenterText : MonoBehaviour, IDialogView
{
#region 索引
private TypewriterByCharacter _dialogText;
private Image _panel;
private Action _nextStep;
#endregion
private bool _skipLineDelay;
private TextParam _textParam;
[Header("跳过阻塞延迟时间/ms")] [SerializeField]
private int delayTime = 300;
private void Awake()
{
InitComponents();
AddSomeListener();
}
private void InitComponents()
{
_dialogText = transform.Find("Dialog Text")?.gameObject.GetComponent<TypewriterByCharacter>();
}
private void AddSomeListener()
{
// 对话显示后有一段延时不可跳过
_dialogText.onTypewriterStart.AddListener(() =>
{
_skipLineDelay = true;
_ = CommonUtil.Delay(delayTime, () => { _skipLineDelay = false; });
});
}
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId, bool isAutoSkip = false)
{
// 把原有对话清掉
ClearBox();
_dialogText.gameObject.SetActive(true);
// 显示内容
_dialogText.ShowText(dialogLine);
// 自动跳过的回调很难独立Remove,只能全部清空再注册,下策
_dialogText.onTextShowed.RemoveAllListeners();
_dialogText.onTextShowed.AddListener(() => EnumEventSystem.Global.Send(DialogEventEnum.LineShown));
_nextStep = nextStep;
// 执行自动跳过结局
if (isAutoSkip)
{
_dialogText.onTextShowed.AddListener(() => _ = CommonUtil.Delay(200, NextStep));
}
}
public void ShowOptions(DialogOption[] dialogueOptions)
{
// 不能出选项
}
public void HideDialog()
{
gameObject.SetActive(false);
}
public bool TrySkipLine(bool isForceSkip)
{
if (_skipLineDelay && !isForceSkip)
{
return true;
}
if (_dialogText.isShowingText)
{
_dialogText.SkipTypewriter();
return true;
}
return false;
}
public void NextStep()
{
if (_nextStep == null) return;
ClearBox();
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
var next = _nextStep;
_nextStep = null;
next.Invoke();
}
public void OnLocalizationChanged(string value)
{
_textParam = JsonUtil.ReadBeanByText<TextParam>(value);
var tmp = _dialogText.GetComponent<TMP_Text>();
// 修改对话文本
tmp.characterSpacing = _textParam.charSpace;
}
private void ClearBox()
{
gameObject.SetActive(true);
_dialogText?.TextAnimator.SetText("");
}
}
}