Use the NetworkReader and NetworkWriter classes to write data to byte streams.
The Multiplayer High Level API is built using these classes, and uses them extensively. However, you can use them directly if you want to implement your own custom transport functionality. They have specific serialization functions for many Unity types (See NetworkWriter.Write for the full list of types).
To use the classes, create a writer instance, and write individual variables into it. These are serialized internally into a byte array, and this can be sent over the network. On the receiving side it’s important that the reader instance for the byte array reads back the variables in exactly the same order they were written in.
This can be used with the MessageBase class to make byte arrays that contain serialized network messages.
void SendMessage(short msgType, MessageBase msg, int channelId)
{
// write the message to a local buffer
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(msgType);
msg.Serialize(writer);
writer.FinishMessage();
myClient.SendWriter(writer, channelId);
}
This message is correctly formatted so that a message handler function can be invoked for it.
The following code sample is a rather low level demonstration, using the lowest level classes from the high-level API for setting up connectivity.
This is the code for connecting the client and server together:
using UnityEngine;
using UnityEngine.Networking;
public class Serializer : MonoBehaviour {
NetworkServerSimple m_Server;
NetworkClient m_Client;
const short k_MyMessage = 100;
// When using a server instance like this it must be pumped manually
void Update() {
if (m_Server != null)
m_Server.Update();
}
void StartServer() {
m_Server = new NetworkServerSimple();
m_Server.RegisterHandler(k_MyMessage, OnMyMessage);
if (m_Server.Listen(5555))
Debug.Log("Started listening on 5555");
}
void StartClient() {
m_Client = new NetworkClient();
m_Client.RegisterHandler(MsgType.Connect, OnClientConnected);
m_Client.Connect("127.0.0.1", 5555);
}
void OnClientConnected(NetworkMessage netmsg) {
Debug.Log("Client connected to server");
SendMessage();
}
}
The next piece of code sends a message using the network reader and network writer, but uses the message handlers built into these classes:
void SendMessage() { NetworkWriter writer = new NetworkWriter(); writer.StartMessage(k_MyMessage); writer.Write(42); writer.Write(“What is the answer”); writer.FinishMessage(); m_Client.SendWriter(writer, 0); }
void OnMyMessage(NetworkMessage netmsg) { Debug.Log(“Got message, size=” + netmsg.reader.Length); var someValue = netmsg.reader.ReadInt32(); var someString = netmsg.reader.ReadString(); Debug.Log(“Message value=” + someValue + “ Message string=‘“ + someString + ”’”); }
When setting up messages for the message handlers, you should always use NetworkWriter.StartMessage()
(with the message type ID) and NetworkWriter.FinishMessage() calls. When not using the byte arrays, you can skip that step.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.