【Unity&JSON】JsonUtility的多对象读写(4)

本文详细介绍了如何在Unity 5.6版本中使用JsonUtility读取和修改存储在JSON文件中的对象数组。通过示例代码展示了如何从文件加载JSON数据,增加、修改和保存JSON对象,以及快速添加新对象到已有数组中。
摘要由CSDN通过智能技术生成

原文内容来自: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));


_4Update_ObjectArray_JsonUtility
----------------------------------------------------------------------------------------------原代码+注释

// Reading an array with nested object stored in a JSON-formatted text file. Changing objects and adding more objects
// using JsonUtility
// At start: {"highscore":[{"name":"BadBoy","scores":4711}]}
// After step 5: {"highscore":[{"name":"BadBoy","scores":4711},{"name":"MagicMike","scores":8828}]}
// After step 7: {"highscore":[{"name":"BadBoy","scores":4712},{"name":"MadMax","scores":1234},{"name":"Amazing Angus","scores":6454},{"name":"Good Guys","scores":1936}]}

using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

public class _4Update_ObjectArray_JsonUtility : MonoBehaviour {
    void Start () {

        /**
         * 1. Fetch text from file * 从文件中取出 text 文本
         */

        string jsonString = File.ReadAllText (Application.dataPath + "/Resources/Json_UpdateObj

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值