using UnityEngine;
using Newtonsoft.Json;
using System.IO;
public static class JsonManager
{
///
/// 保存数据 PC端
///
/// 需要存储的数据对象
/// 存储为的文件名
/// 存储方式,默认LitJson
public static void SaveData(object data, string fileName)
{
//存储路径
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
//序列化 得到json字符串
string json = "";
json = JsonConvert.SerializeObject(data);
//把json字符串保存成文件
File.WriteAllText(path, json);
}
///
/// 读取数据 PC端
///
/// 类型
///
///
public static T LoadData(string fileName) where T : new()
{
//路径
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
if (!File.Exists(path))
{
path = Application.persistentDataPath + "/" + fileName + ".json";
}
if (!File.Exists(path))
{
return new T();
}
//获取json字符串
string jsonStr = File.ReadAllText(path);
//
T data = new T();
//反序列
data = JsonConvert.DeserializeObject(jsonStr);
return data;
}
}