分化屏幕dialog与对话框dialog

This commit is contained in:
2024-09-18 11:59:51 +08:00
parent 7257c39577
commit 5e7e7bbb0e
7 changed files with 33 additions and 10 deletions

View File

@@ -0,0 +1,100 @@
using System.Collections;
using Dialog;
using Event;
using Event.EventArgs;
using TMPro;
using UnityEngine;
namespace UI
{
public class ScreenDialog : MonoBehaviour
{
public float typingSpeed = 0.05f;
public string introduceText;
private string _currentText = "";
private float _timer;
private int _currentIndex;
private bool _start;
private bool _isBlinking;
public TMP_Text textMeshPro;
public float printTime;
public float maxPrintTime=10;
public bool isTime2Break;
private string _eventToBeExc;
private string _eventArg;
private void Update()
{
if (!_start) return;
printTime += Time.deltaTime;
if (printTime > maxPrintTime)
{
isTime2Break = true;
StartCoroutine(Delay());
}
if (_currentText == introduceText)
{
return;
}
_timer += Time.deltaTime;
if (!(_timer >= typingSpeed)) return;
_timer = 0f;
_currentText += introduceText[_currentIndex];
textMeshPro.text = _currentText;
_currentIndex++;
}
// 沟槽的生命周期
private IEnumerator Delay()
{
yield return new WaitForSeconds(1f);
EventManager.Instance.DialogEventSwitch(_eventToBeExc,_eventArg);
}
private void Start()
{
EventManager.Instance.DialogPop += StartPrinting;
}
private IEnumerator BlinkCursor()
{
while (true)
{
if (isTime2Break)
{
_start = false;
printTime = 0;
isTime2Break = false;
textMeshPro.fontSize = 48.7f;
textMeshPro.text = "+";
break;
}
if (!_isBlinking)
{
textMeshPro.text += "|";
_isBlinking = true;
}
else
{
textMeshPro.text = textMeshPro.text[..^1];
_isBlinking = false;
}
yield return new WaitForSeconds(0.3f);
}
}
private void StartPrinting(DialogPopArgs e)
{
if(!DialogManager.Instance.GetDialogByIndex(e.Index).Type.Equals("screen")) return;
if(printTime != 0) return;
_start = true;
_currentText = "";
introduceText = DialogManager.Instance.GetDialogByIndex(e.Index).Content;
_eventToBeExc = DialogManager.Instance.GetDialogByIndex(e.Index).DialogEvent;
_eventArg = DialogManager.Instance.GetDialogByIndex(e.Index).DialogEventArg;
_currentIndex = 0;
_timer = 0f;
textMeshPro.fontSize = 80;
StartCoroutine(BlinkCursor());
}
}
}