Hi,
We are still working on the high level API for the binary serialization of the package which is why the documentation is missing.
While the package does not explicitly support NativeArray types yet (this is something we plan to add moving forward). You should be able to work around by using adapters. The binary ones have the same API as the JSON version.
Unfortunately there is support for generic type adapters yet. So you will need to have one explicitly for the types you need.
Here is an example for an int array:
class NativeArrayAdapter : IBinaryAdapter<NativeArray<int>>
{
public unsafe void Serialize(UnsafeAppendBuffer* writer, NativeArray<int> value)
{
writer->Add(value.Length);
for (var i = 0; i < value.Length; i++)
writer->Add(value[i]);
}
public unsafe NativeArray<int> Deserialize(UnsafeAppendBuffer.Reader* reader)
{
var length = reader->ReadNext<int>();
var value = new NativeArray<int>(length, Allocator.Persistent);
for (var i = 0; i < length; i++)
value[i] = reader->ReadNext<int>();
return value;
}
}
byte[] ObjectToByteArray<T>(T obj)
{
using (var stream = new UnsafeAppendBuffer(16, 8, Allocator.Temp))
{
unsafe
{
BinarySerialization.ToBinary(&stream, obj, new BinarySerializationParameters
{
UserDefinedAdapters = new List<IBinaryAdapter>
{
new NativeArrayAdapter()
}
});
}
return stream.ToBytes();
}
}
T ByteArrayToObject<T>(byte[] arr)
{
unsafe
{
fixed (byte* ptr = arr)
{
var reader = new UnsafeAppendBuffer.Reader(ptr, arr.Length);
return BinarySerialization.FromBinary<T>(&reader, new BinarySerializationParameters
{
UserDefinedAdapters = new List<IBinaryAdapter>
{
new NativeArrayAdapter()
}
});
}
}
}