• 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 使用完整参考示例
  • 相关阅读:
    vue表格多级列表嵌套数据
    HTML5-企业宣传6款免费源码
    HTML5简介及HTML5的发展前景
    30几个HTML5经典动画应用回顾 让你大饱眼福
    2015年必火的五个Html5移动开发工具推荐
    HTML5几种常见的错误写法
    HTML5实现动画三种方式
    简单的圆形图标鼠标hover效果 | CSS3教程
    函数:声明和表达式
    前端工程之模块化
  • 原文地址:https://www.cnblogs.com/tongy0/p/7597603.html
Copyright © 2020-2023  润新知