当前位置: 首页 > news >正文

河南商丘疫情最新政策关键词优化报价

河南商丘疫情最新政策,关键词优化报价,做U启的网站,iis怎么查看网站的域名文档 Json序列化 脚本序列化 问题 Unity序列化数组时,只能存储基类内容,子类内容缺少。 Unity版本 2019.4.40 原因:Unity序列化不支持多态 测试类 将testarray类序列化时,多态列表personlist只转换了基类数据,子类…

文档

Json序列化
脚本序列化

问题

Unity序列化数组时,只能存储基类内容,子类内容缺少。
Unity版本 2019.4.40
原因:Unity序列化不支持多态

测试类
将testarray类序列化时,多态列表personlist只转换了基类数据,子类数据没有转换

    public class Person{public string name = "1";}[System.Serializable]public class Student : Person{public string grade = "2";}[System.Serializable]public class TestArray{public List<Person> personList;}

测试

private void Start()
{TestArray testArray = new TestArray();testArray.personList = new List<Person>() { new Person(), new Student() };var content = JsonUtility.ToJson(testArray);Debug.Log(content);
}

输出结果
student实例的内容grade没有转换

打印:{"personList":[{"name":"1"},{"name":"1"}]}

解决方法

将多态数组拆分为有具体类型的多个数组
相同类型的元素存储到相同类型的数组中,避免多态
优点:思路简单,效率高
缺点:没有了多态的特性

[System.Serializable]
public class Grade3
{public List<Student> students;public List<Person> persons;
}

解决方法1

JsonUtility可以将多态数组的元素完整转换
利用该特点,将多态数组的子元素转存为类型子元素对应的Json字符串
优点:思路简单
缺点:不同类型的多态数组都需要实现转换逻辑,效率低,频繁使用JsonUtility方法GC多
适用于:数据量较少,子类数量不固定

测试类
解释:自定义类实现ISerializationCallbackReceiver接口,
序列化之前,将多态数组中的元素保存为类型和数据字符串
反序列化之后,遍历类型和数据字符串,利用反射和json反序列化,将数据还原到到多态数组中

    [System.Serializable]public class Grade : ISerializationCallbackReceiver{[NonSerialized]public Person[] personList;[SerializeField]private string[] types;[SerializeField]private string[] datas;public void OnBeforeSerialize()//序列化之前{if (personList != null && personList.Length > 0){var length = personList.Length;types = new string[length];datas = new string[length];for (int i = 0; i < length; i++){types[i] = personList[i].GetType().FullName;//存储子元素类型datas[i] = JsonUtility.ToJson(personList[i]);//存储子元素转换的字符串}}}public void OnAfterDeserialize(){if (types != null && datas != null){var length = types.Length;personList = new Person[length];Dictionary<string, Type> typeDic = new Dictionary<string, Type>();//缓存类型避免频繁获取for (int i = 0; i < length; i++){if (!typeDic.ContainsKey(types[i]))typeDic.Add(types[i], Type.GetType(types[i]));var type = typeDic[types[i]];if (type != null && typeof(Person).IsAssignableFrom(type)){Person person = (Person)Activator.CreateInstance(type);//反射创建实例元素JsonUtility.FromJsonOverwrite(datas[i], person);//反序列化数据存放到实例中personList[i] = person;}}}}public void Print()//打印数组元素{for (int i = 0; i < personList.Length; i++){Debug.Log(personList[i]);}}}

测试

private void Start()
{var grade = new Grade();grade.personList = new Person[2] { new Person(), new Student() };var info = JsonUtility.ToJson(grade);Debug.Log(info);
}

输出结果
student实例的内容grade转换

打印:
{
"types":["Test.Person","Test.Student"],
"datas":["{\"name\":\"1\"}","{\"name\":\"1\",\"grade\":\"2\"}"]
}

解决方法2

优点:序列化速度比方法1快,保留多态特性
缺点:子类数量一旦过多,需要编写更多脚本
适用于子类数目较少的,不需要元素的顺序

测试类
解释:序列化时,将多态数组中的数据存放到对应类型的数组中,减少Json方法的调用
反序列化时,将对应数组中的数据存放到多态数组中

    public class Grade2 : ISerializationCallbackReceiver{[NonSerialized]public List<Person> runtimes;//不需要序列化多态数组[SerializeField] Student[] students;[SerializeField] Person[] persons;public void OnBeforeSerialize(){this.students = null;this.persons = null;if (runtimes != null && runtimes.Count > 0){var length = runtimes.Count;int[] indexArray = new int[length];//索引数组int arrayLength = 0;Type targetType = typeof(Student);for (int i = 0; i < length; i++){if (runtimes[i].GetType() == targetType){arrayLength++;indexArray[i] = i;//存放当前Type对应元素的索引}}if (arrayLength > 0){this.students = new Student[arrayLength];//直接设置长度for (int i = 0; i < arrayLength; i++)this.students[i] = runtimes[indexArray[i]] as Student;}arrayLength = 0;targetType = typeof(Person);for (int i = 0; i < length; i++){Type type = runtimes[i].GetType();if (type == targetType){arrayLength++;indexArray[i] = i;}}if (arrayLength > 0){this.persons = new Person[arrayLength];for (int i = 0; i < arrayLength; i++)this.persons[i] = runtimes[indexArray[i]];}}}public void OnAfterDeserialize(){int length = 0;if (students != null)length += students.Length;if (persons != null)length += persons.Length;runtimes = new List<Person>(length);//直接设置列表容量if (students != null)//填充runtimes.AddRange(students);if (persons != null)runtimes.AddRange(persons);}public void Print(){for (int i = 0; i < runtimes.Count; i++){Debug.Log(runtimes[i]);}}}

测试

var grade2 = new Grade2();
grade2.runtimes = new List<Person> { new Person(), new Student() };
var info2 = JsonUtility.ToJson(grade2);
Debug.Log(info2);

输出结果

打印:{"students":[{"name":"1","grade":"2"}],"persons":[{"name":"1"}]}

解决方法3

使用高版本Unity的[SerializeReference]特性
优点:不需要编写多余内容,使用简单,效率比方法1高
缺点:序列化效率差,存储文件大,只支持高版本

测试类

    [System.Serializable]public class Grade4{[SerializeReference]//添加特性public List<Person> runtimes;}

测试

Grade4 grade4 = new Grade4();
grade4.runtimes = new List<Person> { new Person(), new Student() };
var info3 = JsonUtility.ToJson(grade);
Debug.Log(info3);

输出结果

{
"runtimes":[{"rid":1000},{"rid":1001}],
"references":{"version":2,"RefIds":[{"rid":1000,"type":{"class":"Person","ns":"Test","asm":"Assembly-CSharp"},"data":{"name":"1"}},{"rid":1001,"type":{"class":"Student","ns":"Test","asm":"Assembly-CSharp"},"data":{"name":"1","grade":"2"}}]}}

解决方法4

使用Newtonsoft.Json第三方包
优点:功能强大,支持多态数组,使用简单
缺点:第三方导入,效率不高

测试

TestArray testArray5 = new TestArray();
testArray5.personList = new List<Person>() { new Person(), new Student() };JsonSerializerSettings settings = new JsonSerializerSettings();//序列化设置
settings.NullValueHandling = NullValueHandling.Ignore;//空值不保存
settings.TypeNameHandling = TypeNameHandling.Auto;//类型名称自动处理
settings.Formatting = Formatting.Indented;//格式化json文档
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;//忽略循环引用
var content5 = JsonConvert.SerializeObject(testArray, settings);
Debug.Log(content5);

输出结果

打印
{"personList": [{"name": "1"},{"$type": "Test.Student, Assembly-CSharp","grade": "2","name": "1"}]
}
http://www.hkea.cn/news/881195/

相关文章:

  • 无锡市住房城乡建设委网站简单网页设计模板html
  • 武汉市大型的网站制作公司网站ip查询
  • 做仪表行业推广有哪些网站电商网站设计
  • 动静分离网站架构百度售后客服电话24小时
  • 做汽车配件生意的网站佛山seo关键词排名
  • 创意建站推荐百度做广告多少钱一天
  • 巴中网站建设公司百度seo怎么做网站内容优化
  • 查网站备案名称上海网络营销seo
  • 人是用什么做的视频网站网络营销方案设计毕业设计
  • 建设网站考虑因素关键词优化是怎么弄的
  • 陕西营销型网站建设推广普通话的内容简短
  • 做配电箱的专门网站百度指数属于行业趋势及人群
  • 学做网站的网站重庆seo整站优化报价
  • 保定网站设计概述seo推广软件排名
  • 查pv uv的网站网络营销推广服务
  • 怎样让客户做网站优化 保证排名
  • 企业营销型网站做的好网络营销的有哪些特点
  • 网站开发 合同兰州快速seo整站优化招商
  • 网站开发技术现状深圳网络营销推广培训
  • 知名网络公司有哪些河北网站seo
  • 学做网站多少钱关键词难易度分析
  • 传奇如何做网站网站建设策划书案例
  • 龙岗 网站建设深圳信科最好用的搜索神器
  • 动态网站开发日志重庆seo整站优化报价
  • 魔站网站建设微信公众号运营推广方案
  • 好的网站建设公司营销推广外包公司
  • 教育机构做网站素材长尾关键词爱站
  • 做网站选什么系统企业网站seo推广
  • 山东省南水北调建设管理局网站腾讯网qq网站
  • 菏泽做网站公司sem网络营销