当前位置:网站首页>Unity发布webGL的时候JsonConvert.SerializeObject()转换失败
Unity发布webGL的时候JsonConvert.SerializeObject()转换失败
2022-06-25 08:01:00 【先生沉默先】
错误信息如图
火狐报错


谷歌报错


原因: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就填那种类型就好。
}
不会可以评论私信Enjoy

。
边栏推荐
- 紧急行政中止令下达 Juul暂时可以继续在美国销售电子烟产品
- 【总结】1361- package.json 与 package-lock.json 的关系
- Check whether the point is within the polygon
- C language: count the number of words in a paragraph
- Meaning of Jieba participle part of speech tagging
- 2021 "Ai China" selection
- On which platform is it safe to buy shares and open an account? Ask for sharing
- 四、卷积神经网络(Convolution Neural Networks)
- Retrieval model rough hnsw
- What is the difference between TP5 and tp6?
猜你喜欢

Similarity calculation method

Check whether the point is within the polygon

C language "recursive series": recursive implementation of 1+2+3++ n

linux中的mysql有10061错误怎么解决

How to calculate the correlation coefficient and correlation degree in grey correlation analysis?

C language: bubble sort

InfluxDB时序数据库

GPU calculation

【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?

Unity addressable batch management
随机推荐
三、自动终止训练
[515. find the maximum value in each tree row]
A 35 year old Tencent employee was laid off and sighed: a suite in Beijing, with a deposit of more than 7 million, was anxious about unemployment
UEFI:修复 EFI/GPT Bootloader
Hyper-v:Hyper-v 第 1 代或第 2 代虚拟机
Beam search and five optimization methods
openid是什么意思?token是什么意思?
View all listening events on the current page by browser
Day 5 script and UI System
某次比赛wp
Stimulsoft Ultimate呈现报告和仪表板
C language "Recursion Series": recursively realizing the n-th power of X
ICer必须知道的35个网站
Fault: 0x800ccc1a error when outlook sends and receives mail
华泰证券在上面开股票账户安全吗?
Prepare these before the interview. The offer is soft. The general will not fight unprepared battles
compiling stm32f4xx_it.c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
Discrimination of configuration, software configuration items and software configuration management items
打新债真的安全吗? 风险大吗
Data preprocessing: discrete feature coding method