5 Binary Serialization
Albin Corén редактировал(а) эту страницу 2018-07-24 15:16:05 +02:00

This documentation is outdated.

The MLAPI has a built-in binary serializer that serializes and supported fields in structs or classes. The serializer comes from this repository. However, as of v1.0.4 the Serializer uses the BitWriter internally thus supports all the types that the BitWriter supports.

Usage is simple.

byte[] binary = BinarySerializer.Serialize<MyClass>(myClass);
MyClass myClass = BinarySerializer.Deserialize<MyClass>(binary);

The supported data types and their sizes are listed below

Bool, Byte, Double, Single, Int, Long, SByte, Short, UInt, ULong, UShort, String, Vector3, Vector2, Quaternion, BoolArray, ByteArray, DoubleArray, SingleArray, IntArray, LongArray, SByteArray, ShortArray, UIntArray, ULongArray, UShortArray, StringArray, Vector3Array, Vector2Array, QuaternionArray

You can also do serialization as part of your message

//Sending
SendToServer<MyClass>("OnMyClass", "MyChannel", myClassInstance);
//Receiving
private void OnMyClass(uint clientId, byte[] data)
{
    MyClass myClassInstance = DeserializeMessage(data);
    //Do something with myClassInstance
}