错误信息如图
火狐报错
谷歌报错
原因:webGL只能使用Unity自带的序列化工具
没有找到文档
修改方式
序列化字典Dictionary<TKey,TValue>
#region Dictionary序列化,
using System;
using System.Collections;
using System.Collections.Generic;
public class SerializeDictionary
{
public static string Dictionary2Json<TKey, TValue>(Dictionary<TKey, TValue> dic)
{
return JsonUtility.ToJson(new SerializeDictionary<TKey, TValue>(dic));
}
public static Dictionary<TKey, TValue> Json2Dictionary<TKey, TValue>(string str)
{
return JsonUtility.FromJson<SerializeDictionary<TKey, TValue>>(str).ToDictionary();
}
}
[Serializable]
public class SerializeDictionary<TKey, TValue> : ISerializationCallbackReceiver
{
[SerializeField]
List<TKey> keys;
[SerializeField]
List<TValue> values;
Dictionary<TKey, TValue> targetDictionary;
public Dictionary<TKey, TValue> ToDictionary() { return targetDictionary; }
public SerializeDictionary(Dictionary<TKey, TValue> targetDictionary)
{
this.targetDictionary = targetDictionary;
}
public void OnBeforeSerialize()
{
keys = new List<TKey>(targetDictionary.Keys);
values = new List<TValue>(targetDictionary.Values);
}
public void OnAfterDeserialize()
{
var count = Math.Min(keys.Count, values.Count);
targetDictionary = new Dictionary<TKey, TValue>(count);
for (var i = 0; i < count; ++i)
{
targetDictionary.Add(keys[i], values[i]);
}
}
}
#endregion
使用方式
Dictionary<string, string> dc = new Dictionary<string, string>();
dc.Add("Helo","Hello");
dc.Add("Helo1", "Hello");
dc.Add("Helo2", "Hello");
var dicStr= SerializeDictionary.DicToJson(dc);
警告
转出的字符串并不是其他json可以直接反序列化出的字符串需要使用下面的方式重新反序列化
反序列化字典Dictionary
string requesDataStr="从上面转出来的json字符串";
Dictionary<string, List<string>> dicListList = new Dictionary<string, List<string>>();
dicListList = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(requesDataStr);
List<string> dicKeyList = dicListList["keys"];
List<string> dicValueList = dicListList["values"];
var count = Math.Min(dicKeyList.Count, dicKeyList.Count);
var targetdicc = new Dictionary<string, string>(count);
for (var i2 = 0; i2 < count; ++i2)
{
targetdicc.Add(dicKeyList[i2], dicValueList[i2]);
}
//targetdicc是从上面json转出来的Dictionary
序列化List
//是可以反序列化的
JsonConvert.DeserializeObject<T>(string);
// List<T>
[Serializable]
public class SerializationList<T>
{
[SerializeField]
List<T> targetList;
public List<T> ToList() { return targetList; }
public SerializationList(List<T> target)
{
this.targetList = target;
}
}
public class SerializeList
{
public static string List2Json<T>(List<T> l)
{
return JsonUtility.ToJson(new SerializationList<T>(l));
}
public static List<T> Json2List<T>(string str)
{
return JsonUtility.FromJson<SerializationList<T>>(str).ToList();
}
}
使用
// Start is called before the first frame update
void Start()
{
List<string> liststr = new List<string>();
liststr.Add("dsd");
liststr.Add("ds1d");
liststr.Add("d1sd");
string listInfo = SerializeList.List2Json(liststr);//序列化
Debug.Log(listInfo);
List<string> list2 = new List<string>();
List<string> c = SerializeList.Json2List<string>(listInfo);//反序列化,尖括号里面是那种类型的list就填那种类型就好。
}