添加对话框事件以及对话持久化文件
This commit is contained in:
3
Assets/Scripts/Data.meta
Normal file
3
Assets/Scripts/Data.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3157b26b247048edabb8d3813d750ae5
|
||||
timeCreated: 1726032247
|
||||
5
Assets/Scripts/Data/DialogData.cs
Normal file
5
Assets/Scripts/Data/DialogData.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
public struct DialogData
|
||||
{
|
||||
public int index;
|
||||
public string content;
|
||||
}
|
||||
3
Assets/Scripts/Data/DialogData.cs.meta
Normal file
3
Assets/Scripts/Data/DialogData.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7db3fb11d36041c5adeba19179fb8f80
|
||||
timeCreated: 1726032238
|
||||
12
Assets/Scripts/Event/EventArgs/DialogPopArgs.cs
Normal file
12
Assets/Scripts/Event/EventArgs/DialogPopArgs.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Event/EventArgs/DialogPopArgs.cs.meta
Normal file
3
Assets/Scripts/Event/EventArgs/DialogPopArgs.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29958321a8bd415ab793cc37deb6a859
|
||||
timeCreated: 1726033413
|
||||
6
Assets/Scripts/Event/EventHandler/DialogPopHandler.cs
Normal file
6
Assets/Scripts/Event/EventHandler/DialogPopHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using Event.EventArgs;
|
||||
|
||||
namespace Event.EventHandler
|
||||
{
|
||||
public delegate void DialogPopHandler(DialogPopArgs e);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4803bc41d6fa4ae884b015a21124459e
|
||||
timeCreated: 1726033554
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Assets/Scripts/UI/Dialog.cs
Normal file
42
Assets/Scripts/UI/Dialog.cs
Normal 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/UI/Dialog.cs.meta
Normal file
3
Assets/Scripts/UI/Dialog.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 166525e51f1d41a2addfb1df626096fc
|
||||
timeCreated: 1726032143
|
||||
Reference in New Issue
Block a user