气泡
This commit is contained in:
@@ -15,7 +15,7 @@ namespace AibisDream.FixSystem
|
||||
#region 效果参数
|
||||
|
||||
public float rotateDuration = 0.5f;
|
||||
public float typeSpeed = 0.1f;
|
||||
public float typeSpeed = 0.05f;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -185,6 +185,7 @@ namespace AibisDream
|
||||
if (_dialogViewDict.TryGetValue(dialogViewType, out var view))
|
||||
{
|
||||
_curView.HideDialog();
|
||||
_lastShowView = null;
|
||||
_curView = view;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using AibisDream.Kit;
|
||||
using AibisDream.Utility;
|
||||
using Febucci.UI.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
public class TextBubble : MonoBehaviour
|
||||
{
|
||||
private TypewriterCore _typewriter;
|
||||
|
||||
#region 参数
|
||||
|
||||
[SerializeField] public ActorRole role;
|
||||
[SerializeField] private int maxCharNum;
|
||||
|
||||
#endregion
|
||||
|
||||
private bool _skipLineDelay;
|
||||
|
||||
public bool IsShowingText => _typewriter.isShowingText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_typewriter = GetComponentInChildren<TypewriterCore>();
|
||||
}
|
||||
|
||||
public void ShowLine(string line, Action onTextEnd)
|
||||
{
|
||||
_typewriter.ShowText("");
|
||||
gameObject.SetActive(true);
|
||||
var formattedLine = CommonUtil.SplitString(line, maxCharNum);
|
||||
_typewriter.ShowText(formattedLine);
|
||||
// 对话结束回调
|
||||
_typewriter.onTextShowed.RemoveAllListeners();
|
||||
_typewriter.onTextShowed.AddListener(() => onTextEnd?.Invoke());
|
||||
}
|
||||
|
||||
public void SkipLine()
|
||||
{
|
||||
if (_typewriter.isShowingText)
|
||||
{
|
||||
_typewriter.SkipTypewriter();
|
||||
}
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d97efe2567012354e9482f0774e66b76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Kit;
|
||||
using AibisDream.Utility;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class TextBubbleGroup : MonoBehaviour, IDialogView
|
||||
{
|
||||
private readonly Dictionary<ActorRole, TextBubble> _bubbleDict = new();
|
||||
|
||||
#region 一些索引
|
||||
|
||||
private bool _skipLineDelay;
|
||||
private Action _nextStep;
|
||||
private Action _onTextShowed;
|
||||
private bool _isAutoSkip;
|
||||
private ActorRole _curRole;
|
||||
|
||||
[Header("跳过阻塞延迟时间/ms")] [SerializeField]
|
||||
private int delayTime = 300;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 初始化
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitBubbles();
|
||||
// 注册泡泡
|
||||
DialogCanvasManager.Instance.RegisterDialogView(DialogViewType.Bubble, this);
|
||||
DialogCanvasManager.Instance.SwitchDialogView(DialogViewType.Bubble);
|
||||
|
||||
HideDialog();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
DialogCanvasManager.Instance.SwitchDialogView(DialogViewType.OldBox);
|
||||
DialogCanvasManager.Instance.UnregisterDialogView(DialogViewType.Bubble);
|
||||
}
|
||||
|
||||
private void InitBubbles()
|
||||
{
|
||||
foreach (var bubble in GetComponentsInChildren<TextBubble>(true))
|
||||
{
|
||||
_bubbleDict[bubble.role] = bubble;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, Action onTextShowed = null,
|
||||
bool isAutoSkip = false)
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
|
||||
if (_curRole != character.role)
|
||||
{
|
||||
_bubbleDict[_curRole].Hide();
|
||||
}
|
||||
|
||||
if (_bubbleDict.TryGetValue(character.role, out var bubble))
|
||||
{
|
||||
bubble.ShowLine(dialogLine, OnTextShowed);
|
||||
// 设置延时
|
||||
_skipLineDelay = true;
|
||||
_ = CommonUtil.Delay(delayTime, () => { _skipLineDelay = false; });
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"{character.GetActorName()}的Role没有对应Bubble");
|
||||
return;
|
||||
}
|
||||
|
||||
// 保留信息
|
||||
_nextStep = nextStep;
|
||||
_onTextShowed = onTextShowed;
|
||||
_isAutoSkip = isAutoSkip;
|
||||
_curRole = character.role;
|
||||
}
|
||||
|
||||
public void ShowOptions(DialogOption[] dialogueOptions)
|
||||
{
|
||||
// 没有选项
|
||||
}
|
||||
|
||||
public void HideDialog()
|
||||
{
|
||||
foreach (var bubble in _bubbleDict.Values)
|
||||
{
|
||||
bubble.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TrySkipLine()
|
||||
{
|
||||
if (_skipLineDelay)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_bubbleDict.TryGetValue(_curRole, out var bubble) && bubble.IsShowingText)
|
||||
{
|
||||
bubble.SkipLine();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void NextStep()
|
||||
{
|
||||
if (_nextStep == null) return;
|
||||
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
|
||||
var next = _nextStep;
|
||||
_nextStep = null;
|
||||
|
||||
_bubbleDict[_curRole].Hide();
|
||||
|
||||
next.Invoke();
|
||||
}
|
||||
|
||||
private void OnTextShowed()
|
||||
{
|
||||
// 触发事件
|
||||
EnumEventSystem.Global.Send(DialogEventEnum.LineShowed);
|
||||
// 停止语音
|
||||
_onTextShowed?.Invoke();
|
||||
|
||||
// 执行自动跳过结局
|
||||
if (_isAutoSkip)
|
||||
{
|
||||
_ = CommonUtil.Delay(200, NextStep);
|
||||
_isAutoSkip = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8341b0b94bf446d9b3b3c50a4428ee15
|
||||
timeCreated: 1740146642
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
@@ -134,5 +135,49 @@ namespace AibisDream.Utility
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 拆分字符串为若干行
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="maxCharsPerLine"></param>
|
||||
/// <returns></returns>
|
||||
public static string SplitString(string input, int maxCharsPerLine)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input) || maxCharsPerLine <= 0)
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
var result = new StringBuilder();
|
||||
var lines = input.Replace("<br>", "\n").Split('\n');
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var currentIndex = 0;
|
||||
|
||||
while (currentIndex < line.Length)
|
||||
{
|
||||
var length = Math.Min(maxCharsPerLine, line.Length - currentIndex);
|
||||
result.Append(line.Substring(currentIndex, length));
|
||||
currentIndex += length;
|
||||
|
||||
if (currentIndex < line.Length)
|
||||
{
|
||||
result.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
result.Append('\n');
|
||||
}
|
||||
|
||||
// Remove the last '\n' if needed
|
||||
if (result.Length > 0)
|
||||
{
|
||||
result.Length--;
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user