也是向csv投降了
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
public struct DialogData
|
||||
{
|
||||
public int index;
|
||||
public string content;
|
||||
}
|
||||
90
Assets/Scripts/Data/DialogManager.cs
Normal file
90
Assets/Scripts/Data/DialogManager.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
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 Dialog(int index, string content, string type, string animation, string character, string dialogEvent)
|
||||
{
|
||||
Index = index;
|
||||
Content = content;
|
||||
Type = type;
|
||||
Animation = animation;
|
||||
Character = character;
|
||||
DialogEvent = dialogEvent;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
print(lines);
|
||||
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]
|
||||
);
|
||||
|
||||
_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,6 +3,7 @@
|
||||
public class DialogPopArgs : System.EventArgs
|
||||
{
|
||||
public int Index { get; private set; }
|
||||
public bool IsScreenDialog { get;private set; }
|
||||
|
||||
public DialogPopArgs(int index)
|
||||
{
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using Data;
|
||||
using Event;
|
||||
using Event.EventArgs;
|
||||
using Newtonsoft.Json;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class Dialog : MonoBehaviour
|
||||
public class ScreenDialog : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private string dialogsPath;
|
||||
[SerializeField] private string dialogJson;
|
||||
private Dictionary<int, string> _dialogsMap = new();
|
||||
|
||||
public float typingSpeed = 0.05f;
|
||||
public string IntroduceText;
|
||||
public string introduceText;
|
||||
private string _currentText = "";
|
||||
private float _timer;
|
||||
private int _currentIndex;
|
||||
@@ -27,22 +20,7 @@ namespace UI
|
||||
public float printTime;
|
||||
public float maxPrintTime=10;
|
||||
public bool isTime2Break;
|
||||
private void Awake()
|
||||
{
|
||||
dialogsPath = Application.persistentDataPath + "/" + "Dialogs.json";
|
||||
|
||||
if (File.Exists(dialogsPath))
|
||||
{
|
||||
dialogJson = File.ReadAllText(dialogsPath);
|
||||
_dialogsMap = JsonConvert.DeserializeObject<Dictionary<int, string>>(dialogJson);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dialogsMap.Add(0, "我会视奸你,永远...永远......");
|
||||
dialogJson = JsonConvert.SerializeObject(_dialogsMap, Formatting.Indented);
|
||||
File.WriteAllText(dialogsPath, dialogJson);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
@@ -52,11 +30,11 @@ namespace UI
|
||||
{
|
||||
isTime2Break = true;
|
||||
}
|
||||
if (_currentText == IntroduceText) return;
|
||||
if (_currentText == introduceText) return;
|
||||
_timer += Time.deltaTime;
|
||||
if (!(_timer >= typingSpeed)) return;
|
||||
_timer = 0f;
|
||||
_currentText += IntroduceText[_currentIndex];
|
||||
_currentText += introduceText[_currentIndex];
|
||||
textMeshPro.text = _currentText;
|
||||
_currentIndex++;
|
||||
}
|
||||
@@ -66,12 +44,6 @@ namespace UI
|
||||
EventManager.Instance.DialogPop += StartPrinting;
|
||||
}
|
||||
|
||||
/*public void OpenDialog(DialogPopArgs e)
|
||||
{
|
||||
Debug.Log(_dialogsMap[e.Index]);
|
||||
}*/
|
||||
|
||||
//闪烁光标
|
||||
private IEnumerator BlinkCursor()
|
||||
{
|
||||
while (true)
|
||||
@@ -95,19 +67,19 @@ namespace UI
|
||||
textMeshPro.text = textMeshPro.text[..^1];
|
||||
_isBlinking = false;
|
||||
}
|
||||
yield return new WaitForSeconds(0.3f); // 闪烁速度
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
}
|
||||
}
|
||||
|
||||
// 调用此方法开始打印文字
|
||||
private void StartPrinting(DialogPopArgs e)
|
||||
{
|
||||
if(!e.IsScreenDialog) return;
|
||||
if(printTime != 0) return;
|
||||
_start = true;
|
||||
_currentText = "";
|
||||
|
||||
|
||||
IntroduceText = _dialogsMap[e.Index];
|
||||
|
||||
|
||||
introduceText = DialogManager.Instance.GetDialogByIndex(e.Index).Content;
|
||||
_currentIndex = 0;
|
||||
_timer = 0f;
|
||||
textMeshPro.fontSize = 80;
|
||||
Reference in New Issue
Block a user