当前位置:网站首页>The serialization class in unity is in JSON format
The serialization class in unity is in JSON format
2022-07-26 13:25:00 【wodownload2】
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class JsonTest : MonoBehaviour
{
//[Serializable]
public class Person
{
[SerializeField]
public string name;
public int age;
}
[Serializable]
public class Student : Person
{
public string code;
}
[Serializable]
public class SkillData
{
public List<Person> persons;
public List<Student> students;
}
void Start()
{
//Write();
Read();
}
private static void Read()
{
string json = File.ReadAllText("a.json");
SkillData skillData = new SkillData();
skillData = JsonUtility.FromJson<SkillData>(json);
for (int i = 0; skillData.persons != null && i < skillData.persons.Count; ++i)
{
Debug.Log(skillData.persons[i].name + " " + skillData.persons[i].age);
}
for (int i = 0; skillData.students != null && i < skillData.students.Count; ++i)
{
Debug.Log(skillData.students[i].name + " " + skillData.students[i].age + " " + skillData.students[i].code);
}
}
private static void Write()
{
Person person = new Person(); person.name = "abc"; person.age = 10;
Student student = new Student(); student.name = "abc"; student.age = 123; student.code = "java";
Student student2 = new Student(); student2.name = "abc2"; student2.age = 1234; student2.code = "C#";
SkillData skillData = new SkillData();
skillData.persons = new List<Person>();
skillData.students = new List<Student>();
skillData.persons.Add(person);
skillData.students.Add(student);
skillData.students.Add(student2);
string json = JsonUtility.ToJson(skillData);
List<string> list = new List<string>();
list.Add(json);
File.WriteAllLines("a.json", list);
}
}
json Serialization , Use unity Their own JsonUtility that will do .
Note the serialized class , To add :Serializable Can be serialized to json In the middle :
边栏推荐
- Tupu 3D visual national style design | collision between technology and culture "cool" spark“
- HCIP第十一天比较(BGP的配置、发布)
- Sword finger offer (21): push in and pop-up sequence of stack
- B+ tree index uses (7) matching column prefix, matching value range (19)
- [applet] why can't the onreachbottom event be triggered? (one second)
- 基于ASP.NET的某高校学院档案管理系统
- Kubelet CRI 容器运行时
- Solution: unable to load the file c:\users\user\appdata\roaming\npm\npx PS1, because running scripts is prohibited on this system.
- pomerium
- 学习pinia 介绍-State-Getters-Actions-Plugins
猜你喜欢

Brief introduction of reflection mechanism

Unicode file parsing methods and existing problems

Kubernetes flannel: host-gw mode

Kubernetes APIServer 限流策略

基于Locust框架进行文件上传下载性能测试

如何构建以客户为中心的产品蓝图:来自首席技术官的建议

云智技术论坛工业专场 明天见!

Photoshop (cc2020) unfinished

Probability theory and mathematical statistics

Dimension disaster dimension disaster suspense
随机推荐
B+ tree index uses (7) matching column prefix, matching value range (19)
B+ tree selection index (2) -- MySQL from entry to proficiency (23)
我们被一个 kong 的性能 bug 折腾了一个通宵
【上位机教程】CANopen通信下一体化步进电机与台达PLC(AS228T)的应用
同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web
Leetcode 2119. number reversed twice
基于WebRTC和WebSocket实现的聊天系统
[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication
银行业客户体验管理现状与优化策略分析
Basic sentence structure of English ----- origin
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
一笔画问题(中国邮递员问题)
Oom caused by improper use of multithreading
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
postgresql官网下载出错
LeetCode 263.丑数
B+树挑选索引(2)---mysql从入门到精通(二十三)
Research status and pain points of deep learning 3D human posture estimation at home and abroad
Sword finger offer (21): push in and pop-up sequence of stack
HCIP第十二天笔记整理(BGP联邦、选路规则)