Unity JsonFx 插件使用
2024-10-19 04:26:28
在Unity中使用 JsonFx 插件笔记(提示:以下在 Unity3D v5.4.0 版本 Win 平台下测试成功)
- 下载 JsonFx 插件
注意:JsonFx 插件其实就是一个 dll 文件(如果是其他平台,就是对应的库文件。如:android 平台下就对就应为 JsonFx.a) - 将下载好的 JsonFx 插件放置于项目 Assets/Plugins 目录下
- 在具体要使用的地方添加如下using
using Pathfinding.Serialization.JsonFx;
- 此后就可以正常使用 JsonFx 插件,参考示例如下:
public class Person
{
public string name;
public int age; public Person(): this("", ) { } public Person(string _name, int _age) {
name = _name;
age = _age;
} }//public class Person #endregion public class C_9_9 : MonoBehaviour
{ // Use this for initialization
void Start () { Person john = new Person("John", );
// 将对象序列化成Json字符串
string Json_Text = JsonWriter.Serialize(john);
Debug.Log(Json_Text);
// 将字符串反序列化成对象
// john = JsonReader.Deserialize(Json_Text) as Person; // 提示,使用这种方式没办法正确反序列化成功 john 对象
john = JsonReader.Deserialize<Person>(Json_Text); // 提示,使用这种方式要求 Person 类必需要有一个默认构造函数
Debug.Log("john.name = " + john.name);
Debug.Log("john.age = " + john.age); } } - 至此,序列化与反序列化均已完成。当然这边还可以结合 System.IO 相关操作进行本地存档等相关处理。
参考代码如下:using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Pathfinding.Serialization.JsonFx;
using System.IO; public class Person
{
public string name;
public int age; public Person(): this("", ) { } public Person(string _name, int _age) {
name = _name;
age = _age;
} }//public class Person public class C_9_9 : MonoBehaviour
{ // Use this for initialization
void Start () { Person john = new Person("John", );
// 将对象序列化成Json字符串
string Json_Text = JsonWriter.Serialize(john);
Debug.Log(Json_Text);
// 将字符串反序列化成对象
// john = JsonReader.Deserialize(Json_Text) as Person; // 提示,使用这种方式没办法正确反序列化成功 john 对象
john = JsonReader.Deserialize<Person>(Json_Text); // 提示,使用这种方式要求 Person 类必需要有一个默认构造函数
Debug.Log("john.name = " + john.name);
Debug.Log("john.age = " + john.age); // 这边将刚才序列化后的字符串保存起来
string dataPath = GetDataPath() + "/jsonfx_test/person_john.txt";
File.WriteAllText(dataPath, Json_Text); } // 取得可读写路径
public static string GetDataPath() {
if (RuntimePlatform.IPhonePlayer == Application.platform) {
// iPhone 路径
string path = Application.dataPath.Substring(, Application.dataPath.Length - );
path = path.Substring(, path.LastIndexOf('/'));
return path + "/Documents";
} else if (RuntimePlatform.Android == Application.platform) {
// 安卓路径
//return Application.persistentDataPath + "/";
return Application.persistentDataPath;
} else {
// 其他路径
return Application.dataPath;
}
} }JsonFx 使用完整参考示例
最新文章
- 使用Free Spire.XLS插入图表
- loaded the ";ViewController"; nib but the view outlet was not set.&#39;
- c语言编译器(linux平台下安装c语言环境)一
- CodeForces 656B
- Tomcat Context配置(转)
- Xlib: connection to ";:0.0"; refused by server Xlib: No protocol specified解决方案
- Operating Cisco Router
- 【Linux高频命令专题(19)】vi/vim
- 利用反射自动生成SQL语句(仿Linq)
- 琐碎-同步centos集群的时间
- [转] .NET 3.5中MSChart组件的ImageLocation属性含义
- mysql binlog解析概要
- 【QT相关】Qt Widgets Module
- 无向图的最短路径算法JAVA实现(转)
- Java之多线程
- C#实现设置完整虚拟路径
- Oracle数据库启动出现ORA-27101错误之ORA-19815处理方式及数据备份
- Spring Security(八):2.4.3 Project Modules
- python 字符串 切片
- 深入理解JVM(7)——类加载器
热门文章
- eclipse启动项目
- java校验身份证号码
- docker之安装和基本使用(一)
- pip安装使用详解【转】
- [转载]Selenium実行中にJavaScriptのコードを実行する
- HDU 2181 哈密顿绕行世界问题 (DFS)
- 在Windows中安装Boot2Docker 遇到 Unable to load R3 module 的解决方案
- java.lang.reflect.UndeclaredThrowableExceptionjiang
- 浅谈css中浮动和清除浮动带来的影响
- 【LOJ】 #2132. 「NOI2015」荷马史诗