分化屏幕dialog与对话框dialog
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user