using UnityEngine;
using System;
using System.Text;
using LitJson;
using System.IO;
public class SaveWithJson {
private static SaveWithJson instance;
public static SaveWithJson Instance
{
get
{
if(instance ==null)
{
instance = new SaveWithJson();
}
return instance;
}
}
public string SaveToJson<T>(T t , string UnityAssetSubPath)
{
string filePath = application.dataPath + UnityAssetSubPath;
FileInfo file = new FileInfo(filePath);
string jsonStr = JsonMapper.ToJson(t);
using(System.IO.StreamWriter thefile = new StreamWriter(filePath , false ,Encoding.UTF8))
{
thefile.WriteLine(jsonStr);
}
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
return jsonStr;
}
public T LoadFromJsonFile<T>(string UnityAssetSubPath)
{
try
{
string filePath = application.dataPath + UnityAssetSubPath;
T t = JsonMapper.ToObject<T>(File.ReadAllText(filePath));
if (t==null)
{
Debug.Log("读取Json为空!");
return default(T);
}
return t;
}catch(Exception e)
{
Debug.Log(e);
return default(T);
}
}
}
JsonMapper工具类
最新推荐文章于 2024-08-31 07:29:29 发布