using System.IO; using UnityEngine; using LitJson; using System.Text; namespace SC.XR.Unity.Module_PlatformAccount { public class LitJsonManager : LitJsonMgr { public override void Init(string key, T t) { PlayerPrefs.DeleteKey(key); t = new T(); PlayerPrefs.SetString(key,JsonMapper.ToJson(t)); WriteJson(key, t); } public override T ReadJson(string key, ref T t) { if (!File.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } string filePath = DirectoryPath + key + ".txt"; if (PlayerPrefs.GetString(key) == "") { Init(key, t); } if (!File.Exists(filePath)) { Init(key, t); } StreamReader sr = new StreamReader(filePath); JsonReader jr = new JsonReader(sr); t = JsonMapper.ToObject(jr); sr.Close(); return t; } public override void WriteJson(string key, T t) { base.WriteJson(key, t); string json = ""; json = JsonMapper.ToJson(t); string filePath = DirectoryPath + key + ".txt"; if (!File.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } if (!File.Exists(filePath)) { FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8); sw.Write(json); sw.Close(); fileStream.Close(); } else { StreamWriter sw = new StreamWriter(filePath); sw.Write(json); sw.Close(); } } public override void Save() { base.Save(); } } }