1.需要一个工具类,因为列表和字典 用JsonUtility 是无法转换成json的。
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem;
/**********************************************用于JsonUtility 只能序列化类[包括类中的字典和列表] 但是不能序列化 列表和字典数据 [List<T> 这种直接去序列化就是{ } 空的] ************************************************************/
// List<T>
[Serializable]
publicclassSerialization<T>
{
[SerializeField]
List<T> target;
publicList<T> ToList() {returntarget; }
publicSerialization(List<T> target)
{
this.target = target;
}
}
// Dictionary<TKey, TValue>
[Serializable]
publicclassSerialization<TKey,TValue> :ISerializationCallbackReceiver
{
[SerializeField]
List<TKey> keys;
[SerializeField]
List<TValue> values;
Dictionary<TKey,TValue> target;
publicDictionary<TKey,TValue> ToDictionary()
{
returntarget;
}
publicSerialization(Dictionary<TKey,TValue> target)
{
this.target = target;
}
publicvoidOnBeforeSerialize()
{
keys =newList<TKey>(target.Keys);
values =newList<TValue>(target.Values);
}
publicvoidOnAfterDeserialize()
{
varcount =Math.Min(keys.Count, values.Count);
target =newDictionary<TKey,TValue>(count);
for(vari = 0; i < count; ++i)
{
target.Add(keys[i], values[i]);
}
}
}
2.被序列化的T 类型数据 需要支持序列化 也就是带[Serializable] 关键字
///<summary>
///每个dailyItem的信息
///</summary>
[Serializable]
publicclassDailyItemInfo
{
//奖牌 -1为没得奖牌
publicintmedals = -1;
//日期 【当月的第几天】
publicintdaily = 0;
//周几
publicintweedDay = 0;
}
3.工具类封装
///<summary>
///保存任何数据类的List列表
///</summary>
///<param name="key"></param>
///<param name="data"></param>
publicstaticvoidSaveListData<T>(stringkey,List<T> data)
{
stringstrList =JsonUtility.ToJson(newSerialization<T>(data));
if(string.IsNullOrEmpty(strList))
return;
SaveStringData(key, strList);
}
///<summary>
///得到任何类型的List列表
///</summary>
///<typeparam name="T"></typeparam>
///<param name="key"></param>
///<returns></returns>
publicstaticList<T> GetListData<T>(stringkey)
{
try
{
returnJsonUtility.FromJson<Serialization<T>>(PlayerPrefs.GetString(key)).ToList();
}
catch
{
Debug.LogError("No such data!");
returndefault(List<T>);
}
}
///<summary>
///保存任何数据类的字典
///</summary>
///<param name="key"></param>
///<param name="data"></param>
publicstaticvoidSaveDicData<Tkey,Tvalue>(stringkey,Dictionary<Tkey,Tvalue> data)
{
stringstrDic =JsonUtility.ToJson(newSerialization<Tkey,Tvalue>(data));
if(string.IsNullOrEmpty(strDic))
return;
SaveStringData(key, strDic);
}
///<summary>
///得到任何类型的字典
///</summary>
///<typeparam name="T"></typeparam>
///<param name="key"></param>
///<returns></returns>
publicstaticDictionary<Tkey,Tvalue> GetDicData<Tkey,Tvalue>(stringkey)
{
try
{
returnJsonUtility.FromJson<Serialization<Tkey,Tvalue>>(PlayerPrefs.GetString(key)).ToDictionary();
}
catch
{
Debug.LogError("No such data!");
returndefault(Dictionary<Tkey,Tvalue>);
}
}
4.如何调用
DailyItemInfoinfo = goasDailyItemInfo;
Debug.Log("日期 = "+ info.daily +" 被点击");
List<DailyItemInfo> aa =newList<DailyItemInfo>();
aa.Add(info);
aa.Add(info);
GameData.SaveListData("666", aa);
varbb =GameData.GetListData<DailyItemInfo>("666");
Debug.Log(bb);
Dictionary<string,DailyItemInfo> cc =newDictionary<string,DailyItemInfo>();
cc["1"] = info;
cc["2"] = info;
GameData.SaveDicData("777", cc);
vardd =GameData.GetDicData<string,DailyItemInfo>("777");
Debug.Log(dd);
网友评论