Unity JsonFx 插件使用

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

  1. 下载 JsonFx 插件
    注意:JsonFx 插件其实就是一个 dll 文件(如果是其他平台,就是对应的库文件。如:android 平台下就对就应为 JsonFx.a)
  2. 将下载好的 JsonFx 插件放置于项目 Assets/Plugins 目录下
  3. 在具体要使用的地方添加如下using
    1 using Pathfinding.Serialization.JsonFx;
  4. 此后就可以正常使用 JsonFx 插件,参考示例如下:
     1 public class Person
     2 {
     3     public string name;
     4     public int age;
     5 
     6     public Person(): this("", 1) {
     7 
     8     }
     9 
    10     public Person(string _name, int _age) {
    11         name = _name;
    12         age  = _age;
    13     }
    14 
    15 }//public class Person
    16 
    17 #endregion
    18 
    19 public class C_9_9 : MonoBehaviour
    20 {
    21 
    22     // Use this for initialization
    23     void Start () {
    24         
    25         Person john          = new Person("John", 19);
    26         // 将对象序列化成Json字符串
    27         string Json_Text     = JsonWriter.Serialize(john);
    28         Debug.Log(Json_Text);
    29         // 将字符串反序列化成对象
    30       //  john               = JsonReader.Deserialize(Json_Text) as Person; // 提示,使用这种方式没办法正确反序列化成功 john 对象
    31         john                 = JsonReader.Deserialize<Person>(Json_Text);   // 提示,使用这种方式要求 Person 类必需要有一个默认构造函数
    32         Debug.Log("john.name = " + john.name);
    33         Debug.Log("john.age  = " + john.age);
    34 
    35     }
    36 
    37 }
  5. 至此,序列化与反序列化均已完成。当然这边还可以结合 System.IO 相关操作进行本地存档等相关处理。
    参考代码如下:
     1 using UnityEngine;
     2 using System.Collections;
     3 using UnityEngine.UI;
     4 using Pathfinding.Serialization.JsonFx;
     5 using System.IO;
     6 
     7 public class Person
     8 {
     9     public string name;
    10     public int age;
    11 
    12     public Person(): this("", 1) {
    13 
    14     }
    15 
    16     public Person(string _name, int _age) {
    17         name = _name;
    18         age  = _age;
    19     }
    20 
    21 }//public class Person
    22 
    23 public class C_9_9 : MonoBehaviour
    24 {
    25 
    26     // Use this for initialization
    27     void Start () {
    28 
    29         Person john          = new Person("John", 19);
    30         // 将对象序列化成Json字符串
    31         string Json_Text     = JsonWriter.Serialize(john);
    32         Debug.Log(Json_Text);
    33         // 将字符串反序列化成对象
    34       //  john               = JsonReader.Deserialize(Json_Text) as Person; // 提示,使用这种方式没办法正确反序列化成功 john 对象
    35         john                 = JsonReader.Deserialize<Person>(Json_Text);   // 提示,使用这种方式要求 Person 类必需要有一个默认构造函数
    36         Debug.Log("john.name = " + john.name);
    37         Debug.Log("john.age  = " + john.age);
    38 
    39         // 这边将刚才序列化后的字符串保存起来
    40         string dataPath      = GetDataPath() + "/jsonfx_test/person_john.txt";
    41         File.WriteAllText(dataPath, Json_Text);
    42 
    43     }
    44 
    45     // 取得可读写路径
    46     public static string GetDataPath() {
    47         if (RuntimePlatform.IPhonePlayer == Application.platform) {
    48             // iPhone 路径
    49             string path                    = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
    50             path                           = path.Substring(0, path.LastIndexOf('/'));
    51             return path + "/Documents";
    52         } else if (RuntimePlatform.Android == Application.platform) {
    53             // 安卓路径
    54             //return Application.persistentDataPath + "/";
    55             return Application.persistentDataPath;
    56         } else {
    57             // 其他路径
    58             return Application.dataPath;
    59         }
    60     }
    61 
    62 }
    JsonFx 使用完整参考示例

     

转载于:https://www.cnblogs.com/tongy0/p/7597603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值