当前位置:网站首页>Unity中序列化类为json格式
Unity中序列化类为json格式
2022-07-26 13:16: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的序列化,使用unity自己的JsonUtility即可。
注意序列化的类,要加上:Serializable才能序列化到json中去:
边栏推荐
- B+ tree index use (9) grouping, back to table, overlay index (21)
- Flutter dart generates a list of random numbers that are not repeated in n intervals
- Why do you want to make "secret comments"?
- Extra (5) - MySQL execution plan (51)
- Some practical operations of vector
- JVM: what does the class loading subsystem do? What is it made of? What eight part essay do you need to remember?
- Probability theory and mathematical statistics
- Does anyone know where the retract of flinksql can be specified? Only api code settings can be seen in online materials
- 牛客刷SQL---2
- 子组件触发父组件自定义事件(defineEmits):子组件传值给父组件
猜你喜欢

从其他文件触发pytest.main()注意事项

A college archives management system based on asp.net

【TypeScript】TypeScript常用类型(上篇)

《Kotlin系列》之MVVM架构封装(kotlin+mvvm)

一笔画问题(中国邮递员问题)

postgresql官网下载出错

【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯

jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?

Example of establishing socket communication with Siemens PLC based on C # open TCP communication

SLAM 02.整体框架
随机推荐
【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
B+ tree (3) clustered index, secondary index -- MySQL from entry to proficiency (XV)
B+ tree index use (9) grouping, back to table, overlay index (21)
Precautions for triggering pytest.main() from other files
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
我们被一个 kong 的性能 bug 折腾了一个通宵
Qualcomm once again "bet" on Zhongke Chuangda to challenge the full stack solution of intelligent driving software and hardware
一笔画问题(中国邮递员问题)
B+ tree index uses (7) matching column prefix, matching value range (19)
[5g] what are Cu and Du in 5g?
MySQL data directory (3) -- table data structure MyISAM (XXVI)
Activity.onStop() 延迟10秒?精彩绝伦的排查历程
Unicode文件解析方法及存在问题
Flutter textfield sets the height and automatically wraps lines, and the rounded border removes the underline
PostgreSQL official website download error
B+ tree selection index (1) -- MySQL from entry to proficiency (22)
Mysql数据目录(1)---数据库结构(二十四)
Router. Push(), router. Reply(), router. Go()
基于Locust框架进行文件上传下载性能测试