在Unity中使用 JsonFx 插件笔记(提示:以下在 Unity3D v5.4.0 版本 Win 平台下测试成功)

  1. 下载 JsonFx 插件
    注意:JsonFx 插件其实就是一个 dll 文件(如果是其他平台,就是对应的库文件。如:android 平台下就对就应为 JsonFx.a)
  2. 将下载好的 JsonFx 插件放置于项目 Assets/Plugins 目录下
  3. 在具体要使用的地方添加如下using
     using Pathfinding.Serialization.JsonFx;
  4. 此后就可以正常使用 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); } }
  5. 至此,序列化与反序列化均已完成。当然这边还可以结合 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 使用完整参考示例

最新文章

  1. 使用Free Spire.XLS插入图表
  2. loaded the &quot;ViewController&quot; nib but the view outlet was not set.&#39;
  3. c语言编译器(linux平台下安装c语言环境)一
  4. CodeForces 656B
  5. Tomcat Context配置(转)
  6. Xlib: connection to &quot;:0.0&quot; refused by server Xlib: No protocol specified解决方案
  7. Operating Cisco Router
  8. 【Linux高频命令专题(19)】vi/vim
  9. 利用反射自动生成SQL语句(仿Linq)
  10. 琐碎-同步centos集群的时间
  11. [转] .NET 3.5中MSChart组件的ImageLocation属性含义
  12. mysql binlog解析概要
  13. 【QT相关】Qt Widgets Module
  14. 无向图的最短路径算法JAVA实现(转)
  15. Java之多线程
  16. C#实现设置完整虚拟路径
  17. Oracle数据库启动出现ORA-27101错误之ORA-19815处理方式及数据备份
  18. Spring Security(八):2.4.3 Project Modules
  19. python 字符串 切片
  20. 深入理解JVM(7)——类加载器

热门文章

  1. eclipse启动项目
  2. java校验身份证号码
  3. docker之安装和基本使用(一)
  4. pip安装使用详解【转】
  5. [转载]Selenium実行中にJavaScriptのコードを実行する
  6. HDU 2181 哈密顿绕行世界问题 (DFS)
  7. 在Windows中安装Boot2Docker 遇到 Unable to load R3 module 的解决方案
  8. java.lang.reflect.UndeclaredThrowableExceptionjiang
  9. 浅谈css中浮动和清除浮动带来的影响
  10. 【LOJ】 #2132. 「NOI2015」荷马史诗