Last active
April 9, 2024 13:04
-
-
Save emilioparker/ccdecbaaad46c3abb3124d483a1ddc72 to your computer and use it in GitHub Desktop.
Flatbuffers Unity Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*Home Page: | |
https://flatbuffers.dev | |
*Repository: | |
https://github.com/google/flatbuffers | |
*Compilers: | |
https://github.com/google/flatbuffers/releases | |
*Instructions on how to write an schema: | |
https://flatbuffers.dev/flatbuffers_guide_writing_schema.html | |
*Compiling schema to c# | |
./flatc -n game.fbs --gen-onefile | |
*Compiling with Object api | |
./flatc -n game.fbs --gen-onefile --gen-object-api | |
*Compiling with support for json. | |
./flatc -n game.fbs --gen-onefile --cs-gen-json-serializer --gen-object-api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ParkerNamespace; | |
enum Status : byte { | |
None = 0, | |
Walking = 1, | |
Attacking = 2, | |
Death = 3 | |
} | |
struct Position { | |
x:float; | |
y:float; | |
z:float; | |
} | |
struct Item { | |
id:ushort; | |
amount:uint; | |
} | |
table Player { | |
pos:Position; | |
name:string; | |
status : Status; | |
inventory:[Item]; | |
} | |
table Game { | |
id : int; | |
players : [Player]; | |
} | |
root_type Game; | |
file_identifier "GAME"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Net; | |
using UnityEngine; | |
using Google.FlatBuffers; | |
using ParkerNamespace; | |
public class Tests : MonoBehaviour | |
{ | |
public void WriteT() | |
{ | |
GameT game = new GameT | |
{ | |
Id = 5, | |
Players = new List<PlayerT> | |
{ | |
new PlayerT | |
{ | |
Name = "A name", | |
Pos = new PositionT | |
{ | |
X = 0, | |
Y = 0, | |
Z = 2 | |
}, | |
Status = Status.Attacking, | |
Inventory = new List<ItemT> | |
{ | |
new ItemT | |
{ | |
Id = 5, | |
Amount = 45 | |
}, | |
new ItemT | |
{ | |
Id = 78, | |
Amount = 34 | |
} | |
} | |
} | |
} | |
}; | |
var bytes = game.SerializeToBinary(); | |
System.IO.File.WriteAllBytes("game.bytes", bytes); | |
Debug.Log("Saved new game " + bytes.Length); | |
var json = game.SerializeToJson(); | |
System.IO.File.WriteAllText("game.json", json); | |
} | |
public void Write() | |
{ | |
FlatBufferBuilder fbb = new FlatBufferBuilder(1); | |
Player.StartInventoryVector(fbb, 3); | |
Item.CreateItem(fbb, 1, 15); | |
Item.CreateItem(fbb, 3, 150); | |
Item.CreateItem(fbb, 4, 675); | |
var inventoryOffset = fbb.EndVector(); | |
var stringOffset = fbb.CreateString("A cool name"); | |
Player.StartPlayer(fbb); | |
Player.AddName(fbb, stringOffset); | |
Player.AddPos(fbb, Position.CreatePosition(fbb, 1, 4, 5)); | |
Player.AddStatus(fbb, Status.Attacking); | |
Player.AddInventory(fbb, inventoryOffset); | |
var playerOffset = Player.EndPlayer(fbb); | |
Game.StartPlayersVector(fbb, 1); | |
fbb.AddOffset(playerOffset.Value); | |
var playersOffset = fbb.EndVector(); | |
Game.StartGame(fbb); | |
Game.AddId(fbb, 1); | |
Game.AddPlayers(fbb, playersOffset); | |
Offset<Game> offset = Game.EndGame(fbb); | |
fbb.Finish(offset.Value); | |
var bytes = fbb.DataBuffer.ToSizedArray(); | |
System.IO.File.WriteAllBytes("game.bytes", bytes); | |
Debug.Log("Saved bytes " + bytes.Length); | |
} | |
public void ReadJson() | |
{ | |
var data = System.IO.File.ReadAllText("game.json"); | |
var game = GameT.DeserializeFromJson(data); | |
Debug.Log("GameId " + game.Id); | |
} | |
public void Read() | |
{ | |
var bytes = System.IO.File.ReadAllBytes("game.bytes"); | |
Game game = Game.GetRootAsGame(new ByteBuffer(bytes)); | |
Debug.Log("Game id " + game.Id); | |
Debug.Log("players " + game.PlayersLength); | |
for (int i = 0; i < game.PlayersLength; i++) | |
{ | |
var player = game.Players(i).Value; | |
Debug.Log("player name " + player.Name); | |
Debug.Log("player status " + player.Status); | |
Debug.Log("player position " + player.Pos.Value.X); | |
Debug.Log("player inventory " + player.InventoryLength); | |
for (int j = 0; j < player.InventoryLength; j++) | |
{ | |
var item = player.Inventory(j).Value; | |
Debug.Log("id " + item.Id + " amount " + item.Amount); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment