Unity JsonFx 插件使用
在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 使用完整参考示例
Unity JsonFx 插件使用的更多相关文章
- 开发unity DLL插件
最近开发一款设备的SDK,想着要开发unity版本,怎么做呢?首先想到的就是在外部编写相关的驱动程序然后集成成几个dll作为unity的SDK使用了.So,我就开始了unity外部插件的研究之旅了. ...
- (转)Unity Cinemachine插件,实现单目标和多目标之间切换
Unity Cinemachine插件学习笔记,实现单目标和多目标之间切换*版本要求Unity2017.1及以上. 参考资料: [官方] Unity 2017.1正式版发布 Cinemachine插件 ...
- Unity使用JsonFX插件进行序列化
孙广东 2015.6.25 Unity and JSON – Quick Guide: 相比較XML的沉重和密集,Json更加高效. Introduction: 什么是 Json ?假设你从未使用过 ...
- 开源Unity小插件CheatConsole
我们在开发游戏的过程中,通常都需要一些快捷的方式来进行一些非常规的测试,这些功能一般被称作控制台或者GM指令,比如虚幻竞技场中,可以使用~键呼出控制台,输入一些指令即可进行快捷设置,比如设置分辨率,全 ...
- 编写 Unity Editor 插件
Editor Style Viewer 在开发过程中,我喜欢编写一些辅助的Editor插件,方便在游戏开发过程进行调试. 下面是摘自Asset Store的一个查看Unity 默认GUI样式的小工具 ...
- 【转】unity地形插件T4M使用帮助
unity的地形系统在手机游戏中因为效率问题基本无法使用,只能通过T4M这个地形插件来进行优化制作.下面大概讲解一下使用流程及方法. 先中U3D里面用自带的地形系统刷出想要的地形和贴图.贴图可以大概刷 ...
- unity Dotween插件的简单介绍及示例代码
unity里面做插值动画的插件有许多,比较常见的有itween.hotween.dotween.根据大家的反馈和实际体验来说,dotween插件在灵活性.稳定性.易用性上都十分突出.这里简单介绍下它的 ...
- Unity给力插件之ShaderForge(一)
这是一个用来制作shader的插件,也是一个很好的学习shader的工具.这个插件上手很容易,但是要用它来制作理想的Shader,需要下点功夫. 这儿先列举出基础知识,以及我的一些实践.以后我还会继续 ...
- Unity给力插件之MeshBaker
这是一个用来合并网格.材质.贴图的插件. 其实网上也有一些比较详细的使用说明,但是真实操作起来时,总是有一些搞不清bug.而且,作为功能比较全的插件,在Unity版本更新时,也难免会一些不兼容的地方. ...
随机推荐
- 发行NEO的NEP-5合约代币
NEO常见的资产有三种 TOKEN (全局资产) Share (全局资产,股份 ) NEP-5 (合约代币,相当于ETH的ERC20) NEP-5 合约代码 https://github.com/AN ...
- django框架<三>
一.ORM操作 1.django orm创建数据库的方法 (1)指定连接pymysql(python3.x),先配置__init__.py import pymysql pymysql.instal ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
- ACM International Collegiate Programming Contest World Finals 2013
ACM International Collegiate Programming Contest World Finals 2013 A - Self-Assembly 题目描述:给出\(n\)个正方 ...
- DAG blockchain (byteball)
转载参考自: https://www.jinse.com/bitcoin/116184.html https://www.jinse.com/blockchain/116175.html https: ...
- Ubuntu 下查看已安装的软件
Ubuntu 下如何查看已安装的软件 1.查看安装的所有软件 dpkg -l 例如: dpkg -l | grep ftp 2.查看软件安装的路径 dpkg -L | grep ftp 也可以用 wh ...
- leetcode 之Valid Sudoku(七)
判断行.列.九宫格内数字是否重复. 按照行.列.九宫格进行检查即可. bool validSudoku(const vector<vector<char>>& boar ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Python静态代码检查工具Flake8
简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...
- cocos2dx2.x 创建项目
cocos2d-x下载地址:http://www.cocos2d-x.org/download 2.0之后的创建项目比较easy了 第一步,首先 cd cocos2d-x-2.2.1/tools/pr ...