魔改LitJson之Litjson的序列化和反序列化参数扩展

LitJson的用途就不在这里介绍了,很多大牛的博客已经有详细的介绍了。
目前新的LitJson已经支持float类型和Dictory字典类型了。
但是还是有很多类型目前是不支持的。

例如:当我想要把三维和二维数据序列化的时候就没办法支持
以下我添加了序列化和反序列化Vector3,Vector2 ,Vector3Int,Tile的示例。
示例已经可以直接拿去享用。
如果你想要支持其余自定义格式,按照下面的格式添加即可。

Tile,Vector3Int类型为Unity2018版本以上的瓦片地图的数据结构。
如果你对瓦片地图想要更深入的了解,那么可以关注我的其余博文,都有介绍。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System;
using UnityEditor;
using UnityEngine.Tilemaps;

public class LitJsonExtend
{
    /// <summary>
    /// List<Vector3> 序列化Vector3
    /// </summary>

    [InitializeOnLoadMethod]
    static void joinV3Type()
    {
         Action<Vector3,JsonWriter> writeType = (v,w) => {
            w.WriteObjectStart();//开始写入对象
            
            w.WritePropertyName("x");//写入属性名
            w.Write(v.x.ToString());//写入值
            
            w.WritePropertyName("y");
            w.Write(v.y.ToString());

            w.WritePropertyName("z");
            w.Write(v.z.ToString());
            
            w.WriteObjectEnd();
        };
        
        JsonMapper.RegisterExporter<Vector3>((v,w) => {
            writeType(v,w);
        });

        Debug.Log("Vector3加入成功");
    }


    /// <summary>
    /// List<Vector3Int> 序列化Vector3Int
    /// </summary>
    [InitializeOnLoadMethod]
    static void joinV3IntType()
    {
         Action<Vector3Int,JsonWriter> writeType = (v,w) => {
            w.WriteObjectStart();//开始写入对象
            
            w.WritePropertyName("x");//写入属性名
            w.Write(v.x.ToString());//写入值
            
            w.WritePropertyName("y");
            w.Write(v.y.ToString());

            w.WritePropertyName("z");
            w.Write(v.z.ToString());
            
            w.WriteObjectEnd();
        };
        
        JsonMapper.RegisterExporter<Vector3Int>((v,w) => {
            writeType(v,w);
        });

        Debug.Log("Vector3Int加入成功");
    }

    /// <summary>
    /// List<Vector2> 序列化Vector2
    /// </summary>
    [InitializeOnLoadMethod]
    static void joinV2Type()
    {
         Action<Vector2, JsonWriter> writeType = (v,w) => {
            w.WriteObjectStart();//开始写入对象
            
            w.WritePropertyName("x");//写入属性名
            w.Write(v.x.ToString());//写入值
            
            w.WritePropertyName("y");
            w.Write(v.y.ToString());
            
            w.WriteObjectEnd();
        };
        
        JsonMapper.RegisterExporter<Vector2>((v,w) => {
            writeType(v,w);
        });

        Debug.Log("Vector2加入成功");
    }

    /// <summary>
    /// List<Tile> 序列化Tile
    /// </summary>
    [InitializeOnLoadMethod]
    static void joinTileType()
    {
         Action<Tile,JsonWriter> writeType = (v,w) => {
            w.WriteObjectStart();//开始写入对象
            
            // w.WritePropertyName("data");//写入属性名
            // w.Write("");//写入值 
            w.WriteObjectEnd();
        };
        
        JsonMapper.RegisterExporter<Tile>((v,w) => {
            writeType(v,w);
        });

        Debug.Log("Tile加入成功");
    }

}

博文到此结束,相关的LitJson.dll文件在我的博客资源里面有,可以自行取用
网址:https://download.csdn.net/download/lq1340817945/11295567

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lq1340817945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值