LoginSignup
4
4

More than 5 years have passed since last update.

UnityでFlatBuffers

Posted at

概要

お試しでUnityにFlatBuffers入れてみた
c#用のコードはここにある。Assets以下にフォルダ取り込むとよい。

Unityに取り込んだらなんかエラーでる

Assets/FlatBuffers/Table.cs(35,32): error CS0172: Can not compute type of conditional expression as `short' and `int' convert implicitly to each other
// 一行修正したら直った
return vtableOffset < bb.GetShort(vtable) ? bb.GetShort(vtable + vtableOffset) : 0;
-> return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;

サンプルコード

test.fbs
namespace Api.Test;

union Any { Test }

table Test {
    name: string;
}

root_type Test

c#用に変換 $ flatc -n test.fbs

test.cs
void Start () {
    var fbb = new FlatBufferBuilder(1);
    var str = fbb.CreateString("hogehoge");

    Test.StartTest(fbb);
    Test.AddName(fbb, str);
    Debug.Log(fbb.DataBuffer());
    var mon = Test.EndTest(fbb);
    fbb.Finish(mon);
    TestBuffer(fbb.DataBuffer());

}

private void TestBuffer(ByteBuffer bb)
{
    var test = Test.GetRootAsTest(bb);
    Debug.Log(test.Name());
}

結果

簡単な評価だけど動くぽい

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4