Search Unity

How to serialize/deserialise with messagePack

Discussion in 'Multiplayer' started by Colatran, Feb 24, 2020.

  1. Colatran

    Colatran

    Joined:
    Jul 7, 2019
    Posts:
    14
    Hi
    I was falowing this tutorial:


    It works fine,
    But I've read the coments and one person suggests to not use BinaryFormatter, instead to use protobuf or messagepack.
    I was trying messagePack.

    The only two funcions that use BinaryFormatter are these two funcions:
    Code (CSharp):
    1.     byte[] CreateBytes() {
    2.         BinaryFormatter formatter = new BinaryFormatter();
    3.         using (MemoryStream ms = new MemoryStream())
    4.         {
    5.             formatter.Serialize(ms, this.Packages);
    6.             return ms.ToArray();
    7.         }
    8.     }
    9.  
    10.     List<T> ReadBytes(byte[] bytes)
    11.     {
    12.         BinaryFormatter formatter = new BinaryFormatter();
    13.         using (MemoryStream ms = new MemoryStream())
    14.         {
    15.             ms.Write(bytes, 0, bytes.Length);
    16.             ms.Seek(0, SeekOrigin.Begin);
    17.             return (List<T>)formatter.Deserialize(ms);
    18.         }
    And I'm having trouble making this work.
    Any suggestions.
    (Feel free to ask for code of this project)