[Unity][JSON]读取写入玩家数据
2017-11-13 12:06
1561 查看
本文暂时 就 一个 角色 的数据 进行 JSON 的 读取写入 数据。
显示的结果
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/10/b8777ddaf0132f4fdc2656918da57726)
在 如下图所示的 文件夹中,如果没有该文件夹,就新建一个。
用记事本 新建 JSON 文件,用VS 新建JSON 也行。格式如下所示
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/10/fda252d06af3d24dae3c65953c7c55df)
在新场景中,新建 物体,挂上 脚本。运行。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/10/a8979fb732579b9bf7a1679e1830d0dc)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
//Using JSONUtility in Unity 5.3 - Working with JSON in Unity
public class JSONDemon : MonoBehaviour {
string path;
string jsonString;
private void Start()
{
path = Application.streamingAssetsPath + "/Creature.json";
jsonString = File.ReadAllText(path);//读取数据
Creature yyy = JsonUtility.FromJson <Creature>(jsonString);
Debug.Log("-----------------------");
Debug.Log(" "+yyy.Name);
Debug.Log(" " + jsonString);
yyy.Level = 666;
string newyyy = JsonUtility.ToJson(yyy);
Debug.Log("-----------------------");
Debug.Log(" " + jsonString);
Debug.Log(" " + yyy.Level);
File.WriteAllText(path, newyyy);//写入数据
string jsonString1 = File.ReadAllText(path);
Creature yyy1 = JsonUtility.FromJson<Creature>(jsonString1);
Debug.Log("-----------------------");
Debug.Log(" " + jsonString1);
Debug.Log(" " + yyy1.Level);
}
}
[System.Serializable]
public class Creature
{
public string Name;
public int Level;
public int[] Stats;
}
显示的结果
在 如下图所示的 文件夹中,如果没有该文件夹,就新建一个。
用记事本 新建 JSON 文件,用VS 新建JSON 也行。格式如下所示
在新场景中,新建 物体,挂上 脚本。运行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
//Using JSONUtility in Unity 5.3 - Working with JSON in Unity
public class JSONDemon : MonoBehaviour {
string path;
string jsonString;
private void Start()
{
path = Application.streamingAssetsPath + "/Creature.json";
jsonString = File.ReadAllText(path);//读取数据
Creature yyy = JsonUtility.FromJson <Creature>(jsonString);
Debug.Log("-----------------------");
Debug.Log(" "+yyy.Name);
Debug.Log(" " + jsonString);
yyy.Level = 666;
string newyyy = JsonUtility.ToJson(yyy);
Debug.Log("-----------------------");
Debug.Log(" " + jsonString);
Debug.Log(" " + yyy.Level);
File.WriteAllText(path, newyyy);//写入数据
string jsonString1 = File.ReadAllText(path);
Creature yyy1 = JsonUtility.FromJson<Creature>(jsonString1);
Debug.Log("-----------------------");
Debug.Log(" " + jsonString1);
Debug.Log(" " + yyy1.Level);
}
}
[System.Serializable]
public class Creature
{
public string Name;
public int Level;
public int[] Stats;
}
相关文章推荐
- unity中从txt文件中读取解析json数据&&unity中json数据写入txt文件
- [Unity&JSON]LitJSON 读取数组文件,多个数据,玩家得分表数据
- [Unity][JSON][List][JSONUnity]多个角色的数据读取写入JSON数据
- unity使用SimpleJSON读取json数据转存程VO存入list内
- 在unity中如何读取json数据
- 【Python】python和json数据相互转换,json读取和写入,repr和eval()使用
- python 读取单所有json数据写入mongodb(单个)
- Unity 用C#脚本读取JSON文件数据
- Unity中把数据写入到本地Txt和读取本地Txt的数据
- XZ_Swift之加载本地json数据、将json数据写入磁盘、从磁盘读取json数据
- Unity 使用JSON实现本地数据保存和读取
- Unity中从Txt文件读取Json数据
- json文件读取并将数据写入文件
- Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器
- PHP如何把数据写入JSON文件并在另一PHP文件读取JSON数据?
- 【Unity&JSON】.net JSON 数组 序列化和反序列化,并 创建玩家数据列表
- 使用Apache POI读取Excel文件,将读取的数据转换成json写入文本文件
- Unity 数据存储与读取_JSON
- php写入和读取json数据方法
- Unity快捷读取XML、JSON文件