【Unity&JSON】JsonUtility的多对象读写(1)
来源:互联网 发布:上海市行知中学校徽 编辑:程序博客网 时间:2024/06/06 03:20
原文内容来自: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));
_1WriteJSON_basic_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.
// basic - without Object arrays
// creating {"name":"Fabi","level":4711,"tags":["Beginner","Fast"]}
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
public class _1WriteJSON_basic_JsonUtility : MonoBehaviour {
void Start () {
/**
* 1. Create some objects and store them into a JSON string
*/
ObjectData myObject = new ObjectData();
myObject.name = "Fabi6";
myObject.level = 47116;
List<string> tags =new List<string>();//* 一个 string类型的List 名字为 tags 为一个新的 string类型的List
tags.Add ("Beginner6");//* 添加 “Beginner” 到 这个 名字为 tags 的 string类型的List
tags.Add ("Fast6");//* 添加 “Fast” 到 这个 名字为 tags 的 string类型的List
myObject.tags = tags.ToArray(); //* 定义一个 string字符串 数组 名字叫 tags_array ,使得 string类型的List tags通过 ToArray转换成 字符串数组
//* 定义一个 JsonData 名字为generatedJsonString
string jsonString = JsonUtility.ToJson(myObject);//* 使用 JsonMapper.ToJson 转换 类型变量 为数据
Debug.Log (jsonString);//* 显示 JSON数据
// logs {"name":"Fabi","level":4711,"tags":["Beginner","Fast"]}
/**
* 2. Save JSON-formatted string in text file
*/
//* 保存 数据 到 。。。 文件
//System.IO.File.WriteAllText (Application.persistentDataPath + "/saveJSON.json");
System.IO.File.WriteAllText (Application.dataPath + "/Resources/Json_basic.json", jsonString);
}
}
[Serializable]// Unity自带的 序列化
//* ObjectData_LitJson 类,用于存放 成员 变量。 名字、等级、标签。
//* 这个ObjectData_LitJson 类,有一个公共函数 ObjectData_LitJson(string name, int level, string[] tags)
//* 使得ObjectData_LitJson 类的成员 变量 = 形参传递 的参数。
public class ObjectData {
public string name;
public int level;
public string [] tags;
}
----------------------------------------------------------------------------------------------代码图片
----------------------------------------------------------------------------------------------代码运行的结果
----------------------------------------------------------------------------------------------
- 【Unity&JSON】JsonUtility的多对象读写(1)
- 【Unity&JSON】JsonUtility的多对象读写(2)
- 【Unity&JSON】JsonUtility的多对象读写(3)
- 【Unity&JSON】JsonUtility的多对象读写(4)
- 【Unity&JSON】LitJson的多对象读写(1)
- Unity的JsonUtility
- Unity JsonUtility类的用法
- 【Unity&JSON】LitJson的多对象读写(2)
- 【Unity&JSON】LitJson的多对象读写(3)
- 【Unity&JSON】LitJson的多对象读写(4)
- Unity中Json转换类 JsonUtility
- JsonUtility 读取 Json
- JsonUtility解析Json
- 关于 unity如何读写Json的学习
- Unity.JsonUtility序列化指南
- [Unity&JSON]读写JSON父类的成员
- Unity中JsonUtility对List<T>和Dictionary<Key,Value>的序列化
- Unity中关于XML和Json的读写
- css使用checkbox来打造switch开关按钮
- [LeetCode]575. Distribute Candies
- C#正则表达式总结
- requires 'package org.junit 4.12.0' but it could not be found
- 虚函数与虚函数表
- 【Unity&JSON】JsonUtility的多对象读写(1)
- 什么是闭包
- 设计模式(策略模式)
- 【Unity&JSON】JsonUtility的多对象读写(2)
- 【Unity&JSON】JsonUtility的多对象读写(3)
- 【Unity&JSON】JsonUtility的多对象读写(4)
- 谷歌浏览器使用
- CPU和GPU的区别
- php事物的四个特性,以及隔离性的四个级别。