JsonFX Unity3D 如何使用JsonFX
下载地址 https://github.com/jsonfx/jsonfx 点击Download ZIP百度云下载链接:http://pan.baidu.com/s/1c18fwa8 密码:c1c2解压 用 vs 打开JsonFx.Json.sln 编译生成动态库Pathfinding.JsonFx.dll将 Pathfinding.Json
下载地址 https://github.com/jsonfx/jsonfx 点击Download ZIP
百度云下载 链接:http://pan.baidu.com/s/1c18fwa8 密码:c1c2
解压 用 vs 打开JsonFx.Json.sln 编译生成动态库Pathfinding.JsonFx.dll
将 Pathfinding.JsonFx.dll 放入 unity Assets文件内(可新建文件)
使用列子 (转自国外一个博客 地址忘了 与 《Unity5.x游戏开发指南》 综合 )
1.新建一个类
using System.Collections;
using System.Collections.Generic;
[System.Serializable]//为了在unity Inspector栏下显示
public class Sandwich
{
public string name;
public string bread;
public float price;
public List<string> ingredients = new List<string>();
public Sandwich() { }
}
[System.Serializable]
public class Sandwiches
{
public List<Sandwich> sandwiches = new List<Sandwich>();
}
新建脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pathfinding.Serialization.JsonFx;//引入JsonFx
using System.IO;//File类
public class Json : MonoBehaviour {
string mypath = "/data.txt"; //路径
public Sandwiches sandwiches = new Sandwiches(); //要存的数据对象
void Write()
{
//把数据序列化为string
string Json_Text = JsonWriter.Serialize(sandwiches);
//把数据写出到文件
File.WriteAllText(mypath, Json_Text);
}
void Read()
{
//文件是否有效
if (!File.Exists(mypath))
{
return;
}
//把文件读取成string
string originText = File.ReadAllText mypath);
//string序列化成对象
Sandwiches sandwiches = JsonReader.Deserialize<Sandwiches>(originText);
}
}
更多推荐
所有评论(0)