分化屏幕dialog与对话框dialog
This commit is contained in:
7
Assets/Scripts/Dialog/BoxDialog.cs
Normal file
7
Assets/Scripts/Dialog/BoxDialog.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Dialog
|
||||
{
|
||||
public class BoxDialog
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Dialog/BoxDialog.cs.meta
Normal file
3
Assets/Scripts/Dialog/BoxDialog.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78f180e85e3a4ef095495b2760268559
|
||||
timeCreated: 1726626993
|
||||
92
Assets/Scripts/Dialog/DialogManager.cs
Normal file
92
Assets/Scripts/Dialog/DialogManager.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class Dialog
|
||||
{
|
||||
public readonly int Index;
|
||||
public readonly string Content;
|
||||
public readonly string Type;
|
||||
public readonly string Animation;
|
||||
public readonly string Character;
|
||||
public readonly string DialogEvent;
|
||||
public readonly string DialogEventArg;
|
||||
|
||||
public Dialog(int index, string content, string type, string animation, string character, string dialogEvent, string dialogEventArg)
|
||||
{
|
||||
Index = index;
|
||||
Content = content;
|
||||
Type = type;
|
||||
Animation = animation;
|
||||
Character = character;
|
||||
DialogEvent = dialogEvent;
|
||||
DialogEventArg = dialogEventArg;
|
||||
}
|
||||
}
|
||||
|
||||
public class DialogManager : MonoBehaviour
|
||||
{
|
||||
public string filePath = "Assets/Dialog/DialogData.csv";
|
||||
private readonly List<Dialog> _dialog = new();
|
||||
|
||||
private static DialogManager _instance;
|
||||
public static DialogManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance)
|
||||
return _instance;
|
||||
_instance = FindObjectOfType<DialogManager>() ??
|
||||
new GameObject("DialogData").AddComponent<DialogManager>();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadCsv(filePath);
|
||||
_instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCsv(string path)
|
||||
{
|
||||
var lines = File.ReadAllLines(path);
|
||||
for (var i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var values = lines[i].Split(',');
|
||||
|
||||
var dialog = new Dialog(
|
||||
int.Parse(values[0]),
|
||||
values[1],
|
||||
values[2],
|
||||
values[3],
|
||||
values[4],
|
||||
values[5],
|
||||
values[6]
|
||||
);
|
||||
|
||||
_dialog.Add(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
public Dialog GetDialogByIndex(int index)
|
||||
{
|
||||
foreach (var dialog in _dialog.Where(dialogue => dialogue.Index == index))
|
||||
{
|
||||
return dialog;
|
||||
}
|
||||
|
||||
Debug.LogWarning($"Dialog with index {index} not found.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Dialog/DialogManager.cs.meta
Normal file
3
Assets/Scripts/Dialog/DialogManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7db3fb11d36041c5adeba19179fb8f80
|
||||
timeCreated: 1726032238
|
||||
100
Assets/Scripts/Dialog/ScreenDialog.cs
Normal file
100
Assets/Scripts/Dialog/ScreenDialog.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Dialog/ScreenDialog.cs.meta
Normal file
3
Assets/Scripts/Dialog/ScreenDialog.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166525e51f1d41a2addfb1df626096fc
|
||||
timeCreated: 1726032143
|
||||
Reference in New Issue
Block a user