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

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

通信协议-FlatBuffer-C#对象序列化与反序列化_第1张图片

新建测试脚本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[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().Value;

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

	}


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

运行结果如下

通信协议-FlatBuffer-C#对象序列化与反序列化_第2张图片

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

你可能感兴趣的:(Unity,C#,通信协议,FlatBuffer,C#版序列化与反序列化)