基于Unity3D的JSON文件的存储的实现
来源:互联网 发布:恢复手机数据的软件 编辑:程序博客网 时间:2024/06/04 23:16
在unity的开发中,经常会用到Json来存储数据文件。如下我将带领大家来实现一套json文件的存储系统。功能如下:
1.若没有json文件,将创建一个json文件,并赋值为数据类StudentData中的数据;
2.若存在json文件,将读取json文件中的数据,并赋值给数据类StudentData;
3.实现对json文件的增,删,改,查。
(注意:本代码使用LitJson.dll进行解析,需把LitJson.dll放入到Unity的Assets中的任意文件夹中 Json官网:http://www.json.org)
代码如下:
学生数据类StudentData
using System.Collections;using System.Collections.Generic;using UnityEngine;//性别枚举public enum SexType{None,Female,Male}[System.Serializable]//课程分数public class Grade{public float math;public float physics;public float english;}[System.Serializable]//学生类public class StudentData {//学号public float id;//学号public string name;//性别public SexType sexType=SexType.None;//年龄public int age; //是否统考public bool bCylet;//成绩public Grade grade;//存储的文件名public const string FILENAME="StudentData";public StudentData(float id,string name,SexType sexType,int age,bool bCylet,Grade grade){this.id = id;this.name = name;this.sexType = sexType;this.age = age;this.bCylet = bCylet;this.grade = grade;}}Json文件管理
using UnityEngine;using System.Collections;using System.IO;using System.Text;using System;using LitJson;public class JsonMgr : MonoBehaviour { public static JsonMgr _instance; //学生数据类 public StudentData studentData; void Awke() { _instance = this; } void Start() {if (File.Exists(Application.dataPath + "//" + StudentData.FILENAME)) {studentData = ParseFile (Application.dataPath, StudentData.FILENAME); } else {CreateFile(Application.dataPath, StudentData.FILENAME, studentData); } } //创建json文件void CreateFile(string filePath,string fileName,StudentData studentData) { StringBuilder stringBuilder = new StringBuilder(); JsonWriter jsonWriter = new JsonWriter(stringBuilder); jsonWriter.WriteObjectStart(); jsonWriter.WritePropertyName("id");jsonWriter.Write(studentData.id); jsonWriter.WritePropertyName("name");jsonWriter.Write(studentData.name);jsonWriter.WritePropertyName("sexType");jsonWriter.Write(studentData.sexType.ToString());jsonWriter.WritePropertyName("age");jsonWriter.Write(studentData.age);jsonWriter.WritePropertyName("bCylet");jsonWriter.Write(studentData.bCylet);jsonWriter.WritePropertyName ("grade");jsonWriter.WriteArrayStart ();jsonWriter.WriteObjectStart ();jsonWriter.WritePropertyName ("math");jsonWriter.Write (studentData.grade.math);jsonWriter.WriteObjectEnd ();jsonWriter.WriteObjectStart ();jsonWriter.WritePropertyName ("physics");jsonWriter.Write (studentData.grade.physics);jsonWriter.WriteObjectEnd ();jsonWriter.WriteObjectStart ();jsonWriter.WritePropertyName ("english");jsonWriter.Write (studentData.grade.english);jsonWriter.WriteObjectEnd (); jsonWriter.WriteArrayEnd(); jsonWriter.WriteObjectEnd();FileStream fileStream = new FileStream(filePath + "//" + fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine(stringBuilder.ToString()); streamWriter.Close(); fileStream.Close(); fileStream.Dispose(); } //解析json文件private StudentData ParseFile(string filePath, string fileName) {StudentData studentData = null;if (File.Exists(filePath + "//" + fileName)) {float id;string name;SexType sexType=SexType.None;int age;bool bCylet;Grade grade=new Grade(); FileStream fileStream = new FileStream(filePath + "//" + fileName, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader streamReader = new StreamReader(fileStream);string strData = streamReader.ReadToEnd (); JsonData jsonData = JsonMapper.ToObject(strData);id = float.Parse (jsonData ["id"].ToString ());name = jsonData["name"].ToString();sexType = (SexType)Enum.Parse (typeof(SexType), jsonData ["sexType"].ToString ());age = int.Parse (jsonData ["age"].ToString());bCylet = bool.Parse (jsonData ["bCylet"].ToString());grade.math = float.Parse(jsonData ["grade"] [0] ["math"].ToString ());grade.physics= float.Parse(jsonData ["grade"] [1] ["physics"].ToString ());grade.english= float.Parse(jsonData ["grade"] [2] ["english"].ToString ());studentData =new StudentData(id,name,sexType,age,bCylet,grade);}return studentData; } void OnApplicationQuit() {CreateFile(Application.dataPath, StudentData.FILENAME,studentData); }}生成的Json文件
{ "id": 1303070223, "name": "zhangsan", "sexType": "Male", "age": 23, "bCylet": true, "grade": [ { "math": 96 }, { "physics": 99 }, { "english": null } ]}
阅读全文
1 0
- 基于Unity3D的JSON文件的存储的实现
- 基于Unity3D的Xml文件的存储的实现
- 实现基于文件存储的Session类
- 基于Unity3D的对象池的实现
- 基于Unity3D的虚拟摇杆的实现
- 基于Unity3D的相册系统的实现
- 基于Unity3D的循环路径的实现
- 基于Unity3D的滑动面板的实现
- ajax03基于json的实现
- Unity3d 基于NGUI的虚拟摇杆实现
- Unity3D基于UGUI的虚拟摇杆实现
- [原创]实现基于文件存储的Session类
- [原创]实现基于文件存储的Session类
- [原创]实现基于文件存储的Session类
- MongoDB基于GridFS实现文件的分布式存储
- MongoDB基于GridFS实现文件的分布式存储
- 将爬取的数据存储为json文件
- 实现基于json的级联菜单
- C++编程语言变量命名规范
- 机房收费系统之日历控件
- IntelliJ IDEA 缓存和索引介绍和清理方法
- SSL P1597 石子合并问题 题目
- 关于冒泡法的一些问题
- 基于Unity3D的JSON文件的存储的实现
- 基于MVC模式封装一个自己的Structs案例
- 变量的伟大
- Linux-块设备-数据结构
- 你对IT风投了解多少?
- 172. Factorial Trailing Zeroes
- 基于ES6的缩放元素的插件
- 全景拼接代码
- java 结构性模式(七种)