通信协议-FlatBuffer-C#对象序列化与反序列化

40 篇文章 3 订阅

上篇文章介绍了FlatBuffer协议的描述文件的编写和转换。本篇介绍C#版协议的序列化与反序列化操作。

新建测试脚本ReadAndWriteTest脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame.Sample;
using FlatBuffers;
 /*
  * Author:W
  * FlatBuffer C#消息的序列化与反序列化操作
  */
public class ReadAndWriteTest : MonoBehaviour {

	// Use this for initialization
	void Start () {

		var builder = new FlatBufferBuilder(1024);


		/** 序列化 **/
		//武器1对象序列化
		var weaponOneName = builder.CreateString("Sword");
		var weaponOneDamage = 3;
		var sword = Weapon.CreateWeapon(builder, weaponOneName, (short)weaponOneDamage);

		//武器2对象序列化
		var weaponTwoName = builder.CreateString("Axe");
		var weaponTwoDamage = 5;
		var axe = Weapon.CreateWeapon(builder,weaponTwoName,(short)weaponTwoDamage);

		//武器数组的序列化
		var weaps = new Offset<Weapon>[2];
		weaps[0] = sword;
		weaps[1] = axe;

		var weapons = Monster.CreateWeaponsVector(builder,weaps);

		

		//背包物品数组序列化
		Monster.StartInventoryVector(builder, 10);
		for (int i = 9; i >= 0; i--)
		{
			builder.AddByte((byte)i);
		}
		var inv = builder.EndVector();


		//路径点数组的序列化
		Monster.StartPathVector(builder,2);
		Vec3.CreateVec3(builder,1f,2f,3f);
		Vec3.CreateVec3(builder, 4f, 5f, 6f);
		var path = builder.EndVector();

		//怪兽名称序列化
		var name = builder.CreateString("Orc");

		//怪兽对象本身序列化
		Monster.StartMonster(builder);
		//位置【struct】
		Monster.AddPos(builder,Vec3.CreateVec3(builder,1f,2f,3f));
		//血量 【short】
		Monster.AddHp(builder,(short)300);
		//名称【字符串】
		Monster.AddName(builder, name);
		//背包物品【ubyte数组】
		Monster.AddInventory(builder,inv);
		//颜色【枚举类型】
		Monster.AddColor(builder, MyGame.Sample.Color.Red);
		//武器【table数组】
		Monster.AddWeapons(builder,weapons);
		//已装备的武器【联合类型】
		Monster.AddEquippedType(builder, Equipment.Weapon);
		Monster.AddEquipped(builder,axe.Value);
		//路径【struct数组】
		Monster.AddPath(builder,path);
		var orc = Monster.EndMonster(builder);

		builder.Finish(orc.Value);

		//获取ByteBuffer
		var buf = builder.DataBuffer;
		//获取ByteBuffer的字节数组【存储、网络传输】
		byte[] buf2 = builder.SizedByteArray();


		/** 反序列化 **/
		var retBuf = new ByteBuffer(buf2);
		var monster = Monster.GetRootAsMonster(retBuf);

		Debug.Log("monster name="+monster.Name+" Mana="+monster.Mana+" HP="+monster.Hp);
		Debug.Log("monster pos  x= "+monster.Pos.Value.X+" y="+ monster.Pos.Value.Y+" z="+ monster.Pos.Value.Z);

		//获取背包物品总长度
		int invLength = monster.InventoryLength;
		//获取第2个物品
		var invItem = monster.Inventory(1);
		Debug.Log("monster inventtory2 ="+invItem);

		//获取武器
		int weaponLength = monster.WeaponsLength;
		Weapon weapon = (Weapon)monster.Weapons(1);
		Debug.Log("monster weapon2 name="+weapon.Name+" damage="+weapon.Damage);


		//获取已装备的武器
		var unionType = monster.EquippedType;
		if (unionType == Equipment.Weapon)
		{
			var wep = monster.Equipped<Weapon>().Value;

			Debug.Log("monoster equipped name="+wep.Name+" damage="+wep.Damage);
		}

	}


	// Update is called once per frame
	void Update () {
		
	}
}

运行结果如下

项目Demo下载地址:链接:https://pan.baidu.com/s/1yBd9aTg9jVDemuBB9n7cNw 提取码:2cce

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Data菌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值