添加对话框事件以及对话持久化文件

This commit is contained in:
2024-09-11 14:07:23 +08:00
parent 8162e2fe30
commit 8bea97f5ae
12 changed files with 94 additions and 4 deletions

3
Assets/Scripts/Data.meta Normal file
View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3157b26b247048edabb8d3813d750ae5
timeCreated: 1726032247

View File

@@ -0,0 +1,5 @@
public struct DialogData
{
public int index;
public string content;
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7db3fb11d36041c5adeba19179fb8f80
timeCreated: 1726032238

View File

@@ -0,0 +1,12 @@
namespace Event.EventArgs
{
public class DialogPopArgs : System.EventArgs
{
public int Index { get; private set; }
public DialogPopArgs(int index)
{
Index = index;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 29958321a8bd415ab793cc37deb6a859
timeCreated: 1726033413

View File

@@ -0,0 +1,6 @@
using Event.EventArgs;
namespace Event.EventHandler
{
public delegate void DialogPopHandler(DialogPopArgs e);
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4803bc41d6fa4ae884b015a21124459e
timeCreated: 1726033554

View File

@@ -33,10 +33,16 @@ namespace Event
}
public event CameraInterActHandler CameraInterAct;
public event DialogPopHandler DialogPop;
public void OnCameraInterAct(GameObject item)
{
CameraInterAct?.Invoke(new CameraInterActArgs(item));
}
public void OnDialogPop(int index)
{
DialogPop?.Invoke(new DialogPopArgs(index));
}
}
}

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.IO;
using Event;
using Event.EventArgs;
using Newtonsoft.Json;
using UnityEngine;
namespace UI
{
public class Dialog : MonoBehaviour
{
[SerializeField] private string dialogsPath;
[SerializeField] private string dialogJson;
private Dictionary<int,string> _dialogsMap = new();
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, "New Dialog");
dialogJson = JsonConvert.SerializeObject(_dialogsMap, Formatting.Indented);
File.WriteAllText(dialogsPath, dialogJson);
}
}
private void Start()
{
EventManager.Instance.DialogPop += OpenDialog;
}
public void OpenDialog(DialogPopArgs e)
{
Debug.Log(_dialogsMap[e.Index]);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 166525e51f1d41a2addfb1df626096fc
timeCreated: 1726032143