【Unity&JSON】JsonUtility的多对象读写(3)
原文内容来自:Read from and write to external .json files using JsonUtilities or LitJson
文件Unity 版本号5.3,使用时候Unity 版本号5.6
文件分流unity-json-master本文仅作分析,学习用途。
类似下面这样,//* 这样的表示和 LitJson 对应 的代码 相同。
//* 的 分数 +1
通过注释来区别 。主要用于 表示 在 代码中 可以 改变 的数据。
someList.Add (createSubObject ("Amazing Angus6", 64546));
原代码,无注释。
someList.Add (createSubObject ("Amazing Angus", 6454));
_3WriteJson_ObjectArray_JsonUtility
----------------------------------------------------------------------------------------------原代码+注释
// Transform objects to a JSON-formatted string and save it into a file.
// Only works from Unity 5.3 on with its new JSONUtility class.
// storing nested objects in array
// creating {"highscore":[{"name":"MagicMike","scores":8828},{"name":"MadMax","scores":4711}]}
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
public class _3WriteJson_ObjectArray_JsonUtility : MonoBehaviour {
public MainObjectData_JsonUtility mainObject;//声明 * 主对象数据List Json,用于存储 最高得分
public InnerObjectData_JsonUtility innerObject;//声明 * 内部对象数据LitJson,用于存储 玩家 的 名字和得分
//*创建 一个内部对象 List 保存,用户的 名字 和 得分
List<InnerObjectData_JsonUtility> objectList = new List<InnerObjectData_JsonUtility> ();
//*一个内部对象 List 保存,用户的 名字 和 得分
//继承 InnerObjectData_JsonUtility 名字为 createSubObject 类 还是函数?
public InnerObjectData_JsonUtility createSubObject(string name, int scores){
InnerObjectData_JsonUtility myInnerObject = new InnerObjectData_JsonUtility();
myInnerObject.name = name;
myInnerObject.scores = scores;
return myInnerObject;
}
void Start () {
/**
* 1. Create some objects and store them into a JSON string * 创建一些嵌套对象 到 一个数组中,并且 存储 这些对象 到JSON 字符串中
*/
//* 创建 一个内部对象 List 保存,用户的 名字 和 得分
objectList.Add (createSubObject ("MagicMike6", 88286));
objectList.Add (createSubObject ("MadMax6", 47116));
//* 放入主对象数据List Json mainObject 中
mainObject.highscore = objectList.ToArray();
string jsonString = JsonUtility.ToJson(mainObject);//* 转换 主对象数据List Json mainObject 为JSONData jsonString
Debug.Log ("generated JsonString: " + jsonString);// * 输出数据
// logs {"highscore":[{"name":"MagicMike","scores":8828},{"name":"MadMax","scores":4711}]}
/**
* 2. Save JSON-formatted string in text file * 保存 JSON格式 的字符串 到Json_ObjectArray.json 文本中
*/
File.WriteAllText(Application.dataPath+"/Resources/Json_ObjectArray.json", jsonString);
}
}
[Serializable]//unity 自带的序列化
public class MainObjectData_JsonUtility
{//* 主对象数据List Json,用于存储 最高得分
public InnerObjectData_JsonUtility [] highscore;
}
[Serializable]
public class InnerObjectData_JsonUtility
{//* 内部对象数据LitJson,用于存储 玩家 的 名字和得分
public string name;
public int scores;
}
----------------------------------------------------------------------------------------------代码图片
----------------------------------------------------------------------------------------------代码运行的结果
----------------------------------------------------------------------------------------------逻辑图(右键-查看图片-放大)
----------------------------------------------------------------------------------------------