This commit is contained in:
2025-11-05 23:59:55 +08:00
parent 80b26ade3b
commit b10e641105
28 changed files with 1089 additions and 1263 deletions
+1 -1
View File
@@ -140,7 +140,7 @@ namespace AibisDream
private void HandleLayout(string line)
{
// 先去除富文本标签
var width = DialogCanvasManager.Instance.EstimateTextWidth(line.RemoveRichTextTags(), _lineText.fontSize);
var width = BubbleGroup.Instance.EstimateTextWidth(line.RemoveRichTextTags(), _lineText.fontSize);
if (width > maxWidth)
{
_layoutElement.preferredWidth = maxWidth;
@@ -1,18 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using TMPro;
using UnityEngine;
namespace AibisDream
{
public class BubbleGroup : Singleton<BubbleGroup>, IDialogView
public class BubbleGroup : Singleton<BubbleGroup>, IDialogView, ILineView
{
[SerializeField] private TMP_Text widthEstimation;
[SerializeField] private CenterBubble centerBubble;
private Dictionary<ActorRole, Bubble[]> _bubbleDict;
private BubbleFactory _bubbleFactory;
private CancellationTokenSource _autoHideToken;
#region
@@ -178,6 +184,10 @@ namespace AibisDream
public void RunLine(LineSyncToken syncToken)
{
// 先中断原来的事件
_autoHideToken?.Cancel();
_autoHideToken = null;
var character = syncToken.lineInfo.character;
if (TryGetBubble(character.role, character.bubbleIdx, out var bubble))
@@ -185,9 +195,11 @@ namespace AibisDream
// 注册事件
bubble.OnLineShown += syncToken.TextShown;
syncToken.SkipTextAnima += () => bubble.SkipLine();
syncToken.OnAdvance += AfterAdvance;
// 播放文字
bubble.ShowActorName(character);
bubble.ShowLine(syncToken.lineInfo.lineText);
syncToken.state = LineSyncState.PlayText;
// 将非选定的Bubble隐藏
HideDialog(bubble);
}
@@ -197,6 +209,28 @@ namespace AibisDream
HideDialog();
}
}
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();
try
{
await Task.Delay(ConstRef.AutoHideDelay, _autoHideToken.Token);
HideDialog();
}
catch (TaskCanceledException)
{
// 忽略取消
}
}
#endregion
}
+343 -368
View File
@@ -3,380 +3,355 @@ using System.Collections.Generic;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.UI;
using TMPro;
using UnityEngine;
namespace AibisDream
{
public class DialogCanvasManager : Singleton<DialogCanvasManager>
{
#region
private readonly Dictionary<DialogViewType, IDialogView> _dialogViewDict = new();
private IDialogView _defaultView;
private IDialogView _showingView;
private IActionController _autoNext;
private IActionController _block;
private IActionController _hideDialog;
public TMP_Text widthEstimation;
#endregion
#region
[SerializeField] private float blockDuration;
[SerializeField] private float autoSkipDelay;
[SerializeField] private float autoHideDuration;
private bool _isNextAutoSkip;
private bool _isNextBlocked;
#endregion
private readonly List<IUnRegister> _rmvList = new();
public override void OnSingletonInit()
{
InitEvent();
}
private void Start()
{
// 默认对话框直接加载
var centerText = transform.Find("Center Text").GetComponent<IDialogView>();
RegisterDialogView(DialogViewType.CenterText, centerText);
var centerOption = transform.Find("Center Option").GetComponent<IDialogView>();
RegisterDialogView(DialogViewType.CenterOption, centerOption);
var bubbleGroup = transform.Find("Bubble Group").GetComponent<IDialogView>();
RegisterDialogView(DialogViewType.StandardBubble, bubbleGroup);
_defaultView = _dialogViewDict[DialogViewType.StandardBubble];
}
private void InitEvent()
{
// 记录相关
_rmvList.Add(
EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.LineStart,
AddDialogueLine));
_rmvList.Add(
EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.OptionSelected,
AddDialogueLine));
_rmvList.Add(EnumEventSystem.Global.Register(EventEnum.NextYarn, CleanDialogHistory));
// 文本显示事件
_rmvList.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineShown, OnTextShown));
_rmvList.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineEnd, OnTextEnd));
}
private void OnTextShown()
{
Debug.Log("[事件处理] OnTextShown - 文本显示完成事件");
// 在快速模式下,不设置阻塞,因为快速模式会立即处理
if (DialogController.Instance.quickRunMode)
{
Debug.Log("[事件处理] OnTextShown - 快速模式,跳过阻塞设置");
return;
}
// 阻塞部分 - 只在非快速模式下阻塞,确保文本能够显示
_isNextBlocked = true;
_block = ActionKit.Delay(blockDuration, () =>
{
Debug.Log("[事件处理] OnTextShown - 阻塞时间结束");
_isNextBlocked = false;
_block = null;
}
).Start(this);
// 自动跳过部分 - 只在非快速模式下执行
if (DialogController.Instance.autoRunMode || _isNextAutoSkip)
{
Debug.Log("[事件处理] OnTextShown - 设置自动跳过定时器");
_autoNext = ActionKit.Delay(autoSkipDelay, () =>
{
Debug.Log("[事件处理] OnTextShown - 自动跳过定时器触发");
_autoNext = null;
NextStep();
}).Start(this);
_isNextAutoSkip = false;
}
}
private void OnTextEnd()
{
Debug.Log("[事件处理] OnTextEnd - 文本结束事件");
// 清理阻塞定时器
if (_block != null)
{
Debug.Log("[事件处理] OnTextEnd - 清理阻塞定时器");
_block.Deinit();
_block = null;
}
_isNextBlocked = false;
// 清理自动跳过定时器
if (_autoNext != null)
{
Debug.Log("[事件处理] OnTextEnd - 清理自动跳过定时器");
_autoNext.Deinit();
_autoNext = null;
}
Debug.Log("[事件处理] OnTextEnd - 清理完成");
}
public override void OnSingletonDestroy()
{
// 销毁前处理事件
_rmvList.ForEach(item => item.UnRegister());
}
#region
private void OnEnable()
{
EnumEventSystem.Global.Register(InteractionEventEnum.DialogStart, HideDialog);
}
private void OnDisable()
{
EnumEventSystem.Global.Register(InteractionEventEnum.DialogStart, HideDialog);
}
#endregion
#region
/// <summary>
/// 注册对话框
/// </summary>
/// <param name="viewType"></param>
/// <param name="dialogView"></param>
public void RegisterDialogView(DialogViewType viewType, IDialogView dialogView)
{
_dialogViewDict.Add(viewType, dialogView);
}
/// <summary>
/// 注销对话框
/// </summary>
/// <param name="viewType"></param>
public void UnregisterDialogView(DialogViewType viewType)
{
_dialogViewDict.Remove(viewType);
}
#endregion
#region
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId,
bool isAutoSkip = false)
{
Debug.Log($"[DialogCanvasManager] ShowLine - 角色对话框类型: {character.dialogViewType}");
Debug.Log($"[DialogCanvasManager] ShowLine - nextStep是否为null: {nextStep == null}");
_isNextAutoSkip = isAutoSkip;
// 根据DialogViewType确定在哪个对话框显示
if (character.dialogViewType == DialogViewType.Default)
{
Debug.Log("[DialogCanvasManager] ShowLine - 使用默认对话框");
if (_showingView != _defaultView)
{
_showingView?.HideDialog();
}
else
{
_hideDialog?.Deinit();
_hideDialog = null;
}
_showingView = _defaultView;
_showingView.ShowLine(dialogLine, character, nextStep, lineId, isAutoSkip);
}
else if (_dialogViewDict.TryGetValue(character.dialogViewType, out var view))
{
Debug.Log($"[DialogCanvasManager] ShowLine - 使用指定对话框: {view.GetType().Name}");
if (_showingView != view)
{
_showingView?.HideDialog();
}
else
{
_hideDialog?.Deinit();
_hideDialog = null;
}
_showingView = view;
// 对于指定了对话框的,就在指定对话框显示
view.ShowLine(dialogLine, character, nextStep, lineId, isAutoSkip);
}
else
{
Debug.Log($"{character.dialogViewType.ToString()}类型对话框不存在");
}
Debug.Log($"[DialogCanvasManager] ShowLine - 当前_showingView类型: {_showingView?.GetType().Name}");
}
public void ShowOptions(DialogOption[] dialogueOptions)
{
var viewType = dialogueOptions[0].Character.dialogViewType;
// 根据状况选择不同的选择框
if (viewType == DialogViewType.Default)
{
if (_showingView != _defaultView)
{
_showingView?.HideDialog();
}
else
{
_hideDialog?.Deinit();
_hideDialog = null;
}
_showingView = _defaultView;
_showingView.ShowOptions(dialogueOptions);
}
else if(_dialogViewDict.TryGetValue(viewType, out var view))
{
if (_showingView != view)
{
_showingView?.HideDialog();
}
else
{
_hideDialog?.Deinit();
_hideDialog = null;
}
_showingView = view;
_showingView.ShowOptions(dialogueOptions);
}
}
public void HideDialog()
{
_showingView?.HideDialog();
}
public bool TrySkipLine(bool isForceSkip = false)
{
string skipType = isForceSkip ? "强制跳过" : "用户跳过";
Debug.Log($"[跳过操作] {skipType} - 阻塞状态: {_isNextBlocked}");
if (_isNextBlocked)
{
Debug.Log("[跳过操作] ❌ 被阻塞,返回true");
return true;
}
bool result = _showingView != null && _showingView.TrySkipLine(isForceSkip);
Debug.Log($"[跳过操作] {skipType} - 结果: {(result ? " " : " ")}");
return result;
}
public void NextStep()
{
Debug.Log("[对话继续] ===== NextStep开始 =====");
Debug.Log($"[对话继续] 当前显示的对话框类型: {_showingView?.GetType().Name}");
// 在快速模式下,不设置延迟隐藏,避免定时器冲突
if (!DialogController.Instance.quickRunMode)
{
var view = _showingView;
_hideDialog = ActionKit.Delay(autoHideDuration, () =>
{
Debug.Log("[对话继续] 延迟隐藏对话框");
_hideDialog = null;
view?.HideDialog();
}).Start(this);
}
else
{
Debug.Log("[对话继续] 快速模式,跳过延迟隐藏设置");
// 清理可能存在的延迟隐藏定时器
if (_hideDialog != null)
{
_hideDialog.Deinit();
_hideDialog = null;
}
}
Debug.Log("[对话继续] 调用对话框NextStep");
_showingView?.NextStep();
Debug.Log("[对话继续] ===== NextStep完成 =====");
}
public void OnLocalizationChanged(string value)
{
}
public void SwitchDialogView(DialogViewType dialogViewType)
{
if (_dialogViewDict.TryGetValue(dialogViewType, out var view))
{
_defaultView?.HideDialog();
_showingView?.HideDialog();
_defaultView = view;
}
else
{
Debug.Log($"{dialogViewType.ToString()}类型对话框不存在");
}
}
#endregion
#region
private RecordPanel RecordPanel => UIManager.Instance.GetPanel<RecordPanel>();
/// <summary>
/// 记录对话
/// </summary>
/// <param name="line">对话</param>
private void AddDialogueLine(DialogText line)
{
if (line.actor.dialogViewType == DialogViewType.Task)
{
return;
}
RecordPanel.AppendDialogLine(line);
}
private void CleanDialogHistory()
{
RecordPanel.ClearDialog();
}
public void CloseCanvas()
{
gameObject.SetActive(false);
}
public void OpenCanvas()
{
gameObject.SetActive(true);
}
#endregion
public float EstimateTextWidth(string text, float fontSize)
{
widthEstimation.fontSize = fontSize;
widthEstimation.text = text;
widthEstimation.ForceMeshUpdate();
return widthEstimation.preferredWidth;
}
// #region 引用
//
// private readonly Dictionary<DialogViewType, IDialogView> _dialogViewDict = new();
// private IDialogView _defaultView;
// private IDialogView _showingView;
//
// private IActionController _autoNext;
// private IActionController _block;
// private IActionController _hideDialog;
//
// #endregion
//
// #region 参数
//
// [SerializeField] private float blockDuration;
// [SerializeField] private float autoSkipDelay;
// [SerializeField] private float autoHideDuration;
//
// private bool _isNextAutoSkip;
// private bool _isNextBlocked;
//
// #endregion
//
// private readonly List<IUnRegister> _rmvList = new();
//
// public override void OnSingletonInit()
// {
// InitEvent();
// }
//
// private void Start()
// {
// // 默认对话框直接加载
// var centerText = transform.Find("Center Text").GetComponent<IDialogView>();
// RegisterDialogView(DialogViewType.CenterText, centerText);
// var centerOption = transform.Find("Center Option").GetComponent<IDialogView>();
// RegisterDialogView(DialogViewType.CenterOption, centerOption);
// var bubbleGroup = transform.Find("Bubble Group").GetComponent<IDialogView>();
// RegisterDialogView(DialogViewType.StandardBubble, bubbleGroup);
//
// _defaultView = _dialogViewDict[DialogViewType.StandardBubble];
// }
//
// private void InitEvent()
// {
// // 记录相关
// _rmvList.Add(
// EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.LineStart,
// AddDialogueLine));
// _rmvList.Add(
// EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.OptionSelected,
// AddDialogueLine));
// _rmvList.Add(EnumEventSystem.Global.Register(EventEnum.NextYarn, CleanDialogHistory));
//
// // 文本显示事件
// _rmvList.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineShown, OnTextShown));
// _rmvList.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineEnd, OnTextEnd));
// }
//
// private void OnTextShown()
// {
// Debug.Log("[事件处理] OnTextShown - 文本显示完成事件");
//
// // 在快速模式下,不设置阻塞,因为快速模式会立即处理
// if (DialogController.Instance.quickRunMode)
// {
// Debug.Log("[事件处理] OnTextShown - 快速模式,跳过阻塞设置");
// return;
// }
//
// // 阻塞部分 - 只在非快速模式下阻塞,确保文本能够显示
// _isNextBlocked = true;
// _block = ActionKit.Delay(blockDuration, () =>
// {
// Debug.Log("[事件处理] OnTextShown - 阻塞时间结束");
// _isNextBlocked = false;
// _block = null;
// }
// ).Start(this);
//
// // 自动跳过部分 - 只在非快速模式下执行
// if (DialogController.Instance.autoRunMode || _isNextAutoSkip)
// {
// Debug.Log("[事件处理] OnTextShown - 设置自动跳过定时器");
// _autoNext = ActionKit.Delay(autoSkipDelay, () =>
// {
// Debug.Log("[事件处理] OnTextShown - 自动跳过定时器触发");
// _autoNext = null;
// NextStep();
// }).Start(this);
// _isNextAutoSkip = false;
// }
// }
//
// private void OnTextEnd()
// {
// Debug.Log("[事件处理] OnTextEnd - 文本结束事件");
//
// // 清理阻塞定时器
// if (_block != null)
// {
// Debug.Log("[事件处理] OnTextEnd - 清理阻塞定时器");
// _block.Deinit();
// _block = null;
// }
// _isNextBlocked = false;
//
// // 清理自动跳过定时器
// if (_autoNext != null)
// {
// Debug.Log("[事件处理] OnTextEnd - 清理自动跳过定时器");
// _autoNext.Deinit();
// _autoNext = null;
// }
//
// Debug.Log("[事件处理] OnTextEnd - 清理完成");
// }
//
// public override void OnSingletonDestroy()
// {
// // 销毁前处理事件
// _rmvList.ForEach(item => item.UnRegister());
// }
//
// #region 对话结束后隐藏
//
// private void OnEnable()
// {
// EnumEventSystem.Global.Register(InteractionEventEnum.DialogStart, HideDialog);
// }
//
// private void OnDisable()
// {
// EnumEventSystem.Global.Register(InteractionEventEnum.DialogStart, HideDialog);
// }
//
// #endregion
//
// #region 对话框相关处理
//
// /// <summary>
// /// 注册对话框
// /// </summary>
// /// <param name="viewType"></param>
// /// <param name="dialogView"></param>
// public void RegisterDialogView(DialogViewType viewType, IDialogView dialogView)
// {
// _dialogViewDict.Add(viewType, dialogView);
// }
//
// /// <summary>
// /// 注销对话框
// /// </summary>
// /// <param name="viewType"></param>
// public void UnregisterDialogView(DialogViewType viewType)
// {
// _dialogViewDict.Remove(viewType);
// }
//
// #endregion
//
// #region 对话框接口
//
// public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId,
// bool isAutoSkip = false)
// {
// Debug.Log($"[DialogCanvasManager] ShowLine - 角色对话框类型: {character.dialogViewType}");
// Debug.Log($"[DialogCanvasManager] ShowLine - nextStep是否为null: {nextStep == null}");
//
// _isNextAutoSkip = isAutoSkip;
// // 根据DialogViewType确定在哪个对话框显示
// if (character.dialogViewType == DialogViewType.Default)
// {
// Debug.Log("[DialogCanvasManager] ShowLine - 使用默认对话框");
// if (_showingView != _defaultView)
// {
// _showingView?.HideDialog();
// }
// else
// {
// _hideDialog?.Deinit();
// _hideDialog = null;
// }
//
// _showingView = _defaultView;
//
// _showingView.ShowLine(dialogLine, character, nextStep, lineId, isAutoSkip);
// }
// else if (_dialogViewDict.TryGetValue(character.dialogViewType, out var view))
// {
// Debug.Log($"[DialogCanvasManager] ShowLine - 使用指定对话框: {view.GetType().Name}");
// if (_showingView != view)
// {
// _showingView?.HideDialog();
// }
// else
// {
// _hideDialog?.Deinit();
// _hideDialog = null;
// }
//
// _showingView = view;
//
// // 对于指定了对话框的,就在指定对话框显示
// view.ShowLine(dialogLine, character, nextStep, lineId, isAutoSkip);
// }
// else
// {
// Debug.Log($"{character.dialogViewType.ToString()}类型对话框不存在");
// }
//
// Debug.Log($"[DialogCanvasManager] ShowLine - 当前_showingView类型: {_showingView?.GetType().Name}");
// }
//
// public void ShowOptions(DialogOption[] dialogueOptions)
// {
// var viewType = dialogueOptions[0].Character.dialogViewType;
// // 根据状况选择不同的选择框
// if (viewType == DialogViewType.Default)
// {
// if (_showingView != _defaultView)
// {
// _showingView?.HideDialog();
// }
// else
// {
// _hideDialog?.Deinit();
// _hideDialog = null;
// }
// _showingView = _defaultView;
//
// _showingView.ShowOptions(dialogueOptions);
// }
// else if(_dialogViewDict.TryGetValue(viewType, out var view))
// {
// if (_showingView != view)
// {
// _showingView?.HideDialog();
// }
// else
// {
// _hideDialog?.Deinit();
// _hideDialog = null;
// }
// _showingView = view;
// _showingView.ShowOptions(dialogueOptions);
// }
// }
//
// public void HideDialog()
// {
// _showingView?.HideDialog();
// }
//
// public bool TrySkipLine(bool isForceSkip = false)
// {
// string skipType = isForceSkip ? "强制跳过" : "用户跳过";
// Debug.Log($"[跳过操作] {skipType} - 阻塞状态: {_isNextBlocked}");
//
// if (_isNextBlocked)
// {
// Debug.Log("[跳过操作] ❌ 被阻塞,返回true");
// return true;
// }
//
// bool result = _showingView != null && _showingView.TrySkipLine(isForceSkip);
// Debug.Log($"[跳过操作] {skipType} - 结果: {(result ? "✅ 成功" : "❌ 失败")}");
// return result;
// }
//
// public void NextStep()
// {
// Debug.Log("[对话继续] ===== NextStep开始 =====");
// Debug.Log($"[对话继续] 当前显示的对话框类型: {_showingView?.GetType().Name}");
//
// // 在快速模式下,不设置延迟隐藏,避免定时器冲突
// if (!DialogController.Instance.quickRunMode)
// {
// var view = _showingView;
// _hideDialog = ActionKit.Delay(autoHideDuration, () =>
// {
// Debug.Log("[对话继续] 延迟隐藏对话框");
// _hideDialog = null;
// view?.HideDialog();
// }).Start(this);
// }
// else
// {
// Debug.Log("[对话继续] 快速模式,跳过延迟隐藏设置");
// // 清理可能存在的延迟隐藏定时器
// if (_hideDialog != null)
// {
// _hideDialog.Deinit();
// _hideDialog = null;
// }
// }
//
// Debug.Log("[对话继续] 调用对话框NextStep");
// _showingView?.NextStep();
//
// Debug.Log("[对话继续] ===== NextStep完成 =====");
// }
//
// public void OnLocalizationChanged(string value)
// {
// }
//
// #endregion
//
// #region 对话回顾
//
// private RecordPanel RecordPanel => UIManager.Instance.GetPanel<RecordPanel>();
//
// /// <summary>
// /// 记录对话
// /// </summary>
// /// <param name="line">对话</param>
// private void AddDialogueLine(DialogText line)
// {
// if (line.actor.dialogViewType == DialogViewType.Task)
// {
// return;
// }
//
// RecordPanel.AppendDialogLine(line);
// }
//
// private void CleanDialogHistory()
// {
// RecordPanel.ClearDialog();
// }
//
// public void CloseCanvas()
// {
// gameObject.SetActive(false);
// }
//
// public void OpenCanvas()
// {
// gameObject.SetActive(true);
// }
//
// #endregion
}
public enum DialogViewType
@@ -0,0 +1,44 @@
using AibisDream.Kit;
using UnityEngine;
namespace AibisDream
{
public class DialogUIManager : Singleton<DialogUIManager>
{
private CanvasGroup _canvasGroup;
[SerializeField] private LineRunner lineView;
[SerializeField] private OptionView optionView;
public override void OnSingletonInit()
{
_canvasGroup = GetComponent<CanvasGroup>();
}
public void CloseCanvas()
{
gameObject.SetActive(false);
}
public void OpenCanvas()
{
gameObject.SetActive(true);
}
public void HideDialog()
{
// 关闭所有对话框
BubbleGroup.Instance.HideDialog();
}
public void RegisterScreenView(DialogViewType dialogViewType, ILineView screenView)
{
lineView.RegisterView(dialogViewType, screenView);
}
public void UnRegisterScreenView(DialogViewType dialogViewType)
{
lineView.UnRegisterView(dialogViewType);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a38dd6a979b842b280fb73c921999a69
timeCreated: 1761295222