[๐ฌ]
์ ๋ค๋ฆญ ์ ์๋์ฌ๋..?
์ต๋ช
(1.209)
2023-01-11 03:04
์ถ์ฒ 0
using UnityEngine;
using System.IO;
using Newtonsoft.Json;
using UnityEngine.Playables;
public class DataManager : MonoBehaviour
{
private string filePath;
void Start()
{
filePath = Application.persistentDataPath + "SaveFile/saveFile.json";
}
public void SaveData(Object data) //์ง๋ ฌํ, Jsonํ์ผ๋ก ์ ์ฅ
{
// Serialize the data to a JSON string
string jsonData = JsonConvert.SerializeObject(data);
// Write the JSON string to a file
File.WriteAllText(filePath, jsonData);
}
public T LoadData() where T : new() //์ญ์ง๋ ฌํ,
//T๋ ๋งค๊ฐ ๋ณ์๊ฐ ์๋ ์์ฑ์๊ฐ ์์ด์ผ ํจ
{
T data;
// Check if the save file exists
if (File.Exists(filePath))
{
// Read the JSON string from the file
string jsonData = File.ReadAllText(filePath);
// Deserialize the JSON string to a GameData object
data = JsonConvert.DeserializeObject(jsonData);
}
else
{
// If the save file doesn't exist, create a new GameData object
data = new T();
}
return data;
}
}
๋ชจ๋ ๋ฐ์ดํฐํ์
์ ์ฅํ ์ ์๋ ๋ฐ์ดํฐ๋ฉ๋์ ์คํฌ๋ฆฝํธ์ธ๋ฐ LoadData ์ ๋ค๋ฆญ์์ data = new T() ๋ช
๋ น์ค์ด ์๋ฌ๊ฐ ๋ฌ์์. ๊ทธ๋์ ํ์ ์ ์ฝ ์กฐ๊ฑด where ๊ฑธ์๋๋ฐ ์ด๋ ๊ฒ ํ๋๊ฒ ์๋๊ฐ,,?
์ฝ๋๊ฐ ์๋์๊ฐ๋ค
public T LoadData'<'T'>'() where T : new() data = (T)JsonConvert.DeserializeObject(jsonData);