您的位置:首页 > 移动开发 > Unity3D

[Unity][JSON]读取写入玩家数据

2017-11-13 12:06 1561 查看
本文暂时 就 一个 角色 的数据  进行 JSON 的 读取写入 数据。

显示的结果



在 如下图所示的 文件夹中,如果没有该文件夹,就新建一个。

用记事本 新建 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: