How to deserialize with NetworkReader

Hi,

I’ve noticed that NetworkWriter has an example of how to use it on http://docs.unity3d.com/ScriptReference/Networking.NetworkWriter.StartMessage.html

but no example for NetworkReader to read a Message (http://docs.unity3d.com/ScriptReference/Networking.NetworkReader.ReadMessage.html)

Could someone explain how this works? In particular, I don’t understand why the following code doesn’t correctly deserialize the output to what it was originally:

Thanks!

A network message contains a messageId and a size header. When a server or client receives a message, it reads that header information first, then passes the rest of the message contents onto the handler function that was registered with RegisterHandler().

The NetworkReader object passed to the handler function can be used like:

static internal void OnMyMessage(NetworkMessage netMsg)
{
    var testMsg = netMsg.ReadMessage<TestMessage>();
}

this is just a helper for:

        public MSG ReadMessage<MSG>() where MSG : MessageBase, new()
        {
            var msg = new MSG();
            msg.Deserialize(this.reader);
            return msg;
        }

If you want to use NetworkReader and NetworkWriter outside of the client/server context, don’t use StartMessage/FinishMessage.

An example here: