48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using UnityEngine;
|
|
using System.IO;
|
|
using Newtonsoft.Json;
|
|
|
|
public static class JsonDataHandler
|
|
{
|
|
public static string File_Path_Folder => Application.persistentDataPath + "/FILE/";
|
|
static readonly string File_Suffix = ".json";
|
|
public static string File_Path(string file_Name)
|
|
{
|
|
return File_Path_Folder + file_Name + File_Suffix;
|
|
}
|
|
public static void Save<T>(T data, string file_Name, string saver_Name)
|
|
{
|
|
|
|
|
|
var path = File_Path(file_Name);
|
|
if (!File.Exists(path)) Directory.CreateDirectory(File_Path_Folder);
|
|
|
|
var file_Data = JsonConvert.SerializeObject(data, Formatting.Indented);
|
|
// file_Data = $"//SAVER:[{saver_Name}]\n" + file_Data;
|
|
File.WriteAllText(path, file_Data);
|
|
|
|
Debug.Log($"_Save_[{file_Name}{File_Suffix}]_FROM_[{saver_Name}]\n" + path);
|
|
}
|
|
public static bool Try_Load<T>(ref T data, string file_Name)
|
|
{
|
|
var path = File_Path(file_Name);
|
|
if (!File.Exists(path))
|
|
{
|
|
Debug.Log($"Load_[{file_Name}]_NULL");
|
|
return false;
|
|
}
|
|
data = JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(path));
|
|
return true;
|
|
}
|
|
public static T Load<T>(string file_Name)
|
|
{
|
|
var path = File_Path(file_Name);
|
|
if (!System.IO.File.Exists(path))
|
|
{
|
|
Debug.Log($"Load_[{file_Name}]_NULL");
|
|
return default;
|
|
//return T;
|
|
}
|
|
return JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(path));
|
|
}
|
|
} |