Files
aibis-dream/Assets/Scripts/UI/DialogUI/OnelineBox.cs
T
dingyuntian 0168b03234 1.4优化
1. 新建梦境专用场景
2. 增加单行对话框,梦境中全程使用该对话框
3. 增加已选择选项标记功能

TODO
1. 增加梦境选择框
2. 为选项增加已选择表现
2026-01-02 23:19:06 +08:00

120 lines
3.2 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Febucci.UI.Core;
using UnityEngine;
using AibisDream.Framework;
using AibisDream.Utility;
namespace AibisDream
{
public class OnelineBox : MonoBehaviour, ILineView
{
[SerializeField] private TypewriterCore content;
[SerializeField] private GameObject box;
private CancellationTokenSource _autoHideToken;
private event Action OnTextShowed;
// ---------------------------- 显示逻辑 ----------------------------
public void RunLine(LineSyncToken syncToken)
{
// 先中断结束事件
_autoHideToken?.Cancel();
_autoHideToken = null;
// 注册事件
OnTextShowed += syncToken.TextShown;
syncToken.SkipTextAnima += () => content.SkipTypewriter();
syncToken.OnAdvance += AfterAdvance;
// 播放文字
content.TextAnimator.SetText(string.Empty);
box.SetActive(true);
content.ShowText(GetOneLineText(syncToken.lineInfo));
syncToken.TextStart();
}
private string GetOneLineText(LineInfo lineInfo)
{
if (string.IsNullOrEmpty(lineInfo.character.GetActorName()))
{
return lineInfo.lineText;
}
return $"<notype>{lineInfo.character.GetActorName()}: </notype>{lineInfo.lineText}";
}
private async void AfterAdvance()
{
_autoHideToken = new CancellationTokenSource();
try
{
await Task.Delay(ConstRef.AutoHideDelay, _autoHideToken.Token);
HideDialog();
}
catch (TaskCanceledException)
{
// 忽略取消
}
}
public void HideDialog()
{
box.SetActive(false);
}
// ---------------------------- 生命周期 ----------------------------
private void Awake()
{
box.SetActive(false);
}
private void Start()
{
DialogUIManager.Instance.RegisterScreenView(DialogViewType.OnelineBox, this);
RegisterEvents();
}
private void OnDestroy()
{
DialogUIManager.Instance.UnRegisterScreenView(DialogViewType.OnelineBox);
UnregisterEvents();
OnTextShowed = null;
}
private void RegisterEvents()
{
// 全局事件
EnumEventSystem.Global.Register(DialogEventEnum.OptionShow, OnOptionShow);
// 文本事件
content.onTextShowed.AddListener(TextShowed);
}
private void UnregisterEvents()
{
// 全局事件
EnumEventSystem.Global.UnRegister(DialogEventEnum.OptionShow, OnOptionShow);
// 文本事件
content.onTextShowed.RemoveListener(TextShowed);
}
private void TextShowed()
{
OnTextShowed?.Invoke();
OnTextShowed = null;
}
private void OnOptionShow()
{
_autoHideToken?.Cancel();
_autoHideToken = null;
HideDialog();
}
}
}