How to make use of existing serializers in a custom serzializer?
Options
Hi,
We have a custom type that we would like to serialize for storage in the player customProperties. This type contains some Vector3 fields. E.g.:
Thank you
We have a custom type that we would like to serialize for storage in the player customProperties. This type contains some Vector3 fields. E.g.:
class ACustomType { Vector3 foo; Vector3 bar; }I'm writing a custom serializer for this type, but am wondering how I can make use of the existing Vector3 serializers for this.
public static byte[] SerializeACustomType (object o) { ACustomType a = (ACustomType ) o; var byteArrayLength = 2*Vector3ByteSize; byte[] bytes = new byte[byteArrayLength]; int index = 0; //This doesn't work, as Serialise only accepts short/int/float; ExitGames.Client.Photon.Protocol.Serialize(a.foo, bytes, ref index); ExitGames.Client.Photon.Protocol.Serialize(a.bar, bytes, ref index); return bytes; }Is my only option to duplicate the Vector3 serialization code in my custom serializer?
Thank you
0
Best Answer
-
Hi @sosh,
I apologize about the delay, sometimes we miss some discussions.
You can try using binary reader and writer as shown in the plugins custom type example.
Or you can do something like this (replaceVector2
withVector3
and replace missingVector3.Size
with actual size value which should be3 * sizeof(float)
, alsoSubArray
is an extension method I'm using which extracts an array of n (2nd argument) elements starting from index (1st argument)):class ACustomType { Vector2 foo; Vector2 bar; const byte code = (byte)'A'; public const int Size = 2 * Vector2.Size; private static byte[] SerializeACustomType(object customobject) { ACustomType vo = (ACustomType)customobject; return vo.Serialize(); } public byte[] Serialize() { byte[] bytes = new byte[Size]; Protocol.Serialize(this.foo).CopyTo(bytes, 0); Protocol.Serialize(this.bar).CopyTo(bytes, Vector2.Size); return bytes; } private static ACustomType DeserializeACustomType(byte[] bytes) { ACustomType aCustomType = new ACustomType(); aCustomType.foo = Protocol.Deserialize(bytes.SubArray(0, Vector2.Size)) as Vector2; aCustomType.bar = Protocol.Deserialize(bytes.SubArray(Vector2.Size, Vector2.Size)) as Vector2; return aCustomType; } public static void Register() { PhotonPeer.RegisterType(typeof(ACustomType), code, SerializeACustomType, DeserializeACustomType); } }
5
Answers
-
Hi @sosh,
I apologize about the delay, sometimes we miss some discussions.
You can try using binary reader and writer as shown in the plugins custom type example.
Or you can do something like this (replaceVector2
withVector3
and replace missingVector3.Size
with actual size value which should be3 * sizeof(float)
, alsoSubArray
is an extension method I'm using which extracts an array of n (2nd argument) elements starting from index (1st argument)):class ACustomType { Vector2 foo; Vector2 bar; const byte code = (byte)'A'; public const int Size = 2 * Vector2.Size; private static byte[] SerializeACustomType(object customobject) { ACustomType vo = (ACustomType)customobject; return vo.Serialize(); } public byte[] Serialize() { byte[] bytes = new byte[Size]; Protocol.Serialize(this.foo).CopyTo(bytes, 0); Protocol.Serialize(this.bar).CopyTo(bytes, Vector2.Size); return bytes; } private static ACustomType DeserializeACustomType(byte[] bytes) { ACustomType aCustomType = new ACustomType(); aCustomType.foo = Protocol.Deserialize(bytes.SubArray(0, Vector2.Size)) as Vector2; aCustomType.bar = Protocol.Deserialize(bytes.SubArray(Vector2.Size, Vector2.Size)) as Vector2; return aCustomType; } public static void Register() { PhotonPeer.RegisterType(typeof(ACustomType), code, SerializeACustomType, DeserializeACustomType); } }
5 -
Thanks0