using Newtonsoft.Json; using System.Collections.Generic; using System.IO; using TMPro; using UnityEngine; public static class MyJsonManager { /// /// 保存数据 PC端 /// /// 需要存储的数据对象 /// 存储为的文件名 /// 存储方式,默认LitJson public static void SaveData(object data, string fileName) { string path = Application.streamingAssetsPath + "/" + fileName + ".json"; string json = ""; json = JsonConvert.SerializeObject(data); 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(); } string jsonStr = File.ReadAllText(path); T data = new T(); data = JsonConvert.DeserializeObject(jsonStr); return data; } /// /// 加载数据列表 /// /// /// /// public static List LoadListData(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 List(); } string jsonStr = File.ReadAllText(path); List data = new List(); data = JsonConvert.DeserializeObject>(jsonStr); return data; } }