Class NetworkWriter
General purpose serializer for UNET (for serializing data to byte arrays).
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
// Writing data to a NetworkWriter and then
// Converting this to a NetworkReader.
void Start()
{
// The data you add to your writer must be prefixed with a message type.
// This is in the form of a short.
short myMsgType = 143;
NetworkWriter writer = new NetworkWriter();
// You start the message in your writer by passing in the message type.
// This is a short meaning that it will take up 2 bytes at the start of
// your message.
writer.StartMessage(myMsgType);
// You can now begin your message. In this case we will just use strings.
writer.Write("Test data 1");
writer.Write("Test data 2");
writer.Write("Test data 3");
// Make sure to end your message with FinishMessage()
writer.FinishMessage();
// You can now access the data in your writer. ToArray() returns a copy
// of the bytes that the writer is using and AsArray() returns the
// internal array of bytes, not a copy.
byte[] writerData = writer.ToArray();
CreateNetworkReader(writerData);
}
void CreateNetworkReader(byte[] data)
{
// We will create the NetworkReader using the data from our previous
// NetworkWriter.
NetworkReader networkReader = new NetworkReader(data);
// The first two bytes in the buffer represent the size
// of the message. This is equal to the NetworkReader.Length
// minus the size of the prefix.
byte[] readerMsgSizeData = networkReader.ReadBytes(2);
short readerMsgSize = (short)((readerMsgSizeData[1] << 8) + readerMsgSizeData[0]);
Debug.Log(readerMsgSize);
// The message type added in NetworkWriter.StartMessage
// is to be read now. It is a short and so consists of
// two bytes. It is the second two bytes on the buffer.
byte[] readerMsgTypeData = networkReader.ReadBytes(2);
short readerMsgType = (short)((readerMsgTypeData[1] << 8) + readerMsgTypeData[0]);
Debug.Log(readerMsgType);
// If all of your data is of the same type (in this case the
// data on our buffer is comprised of only strings) you can
// read all the data from the buffer using a loop like so.
while (networkReader.Position < networkReader.Length)
{
Debug.Log(networkReader.ReadString());
}
}
}
Namespace: UnityEngine.Networking
Syntax
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkWriter
Constructors
NetworkWriter()
Creates a new NetworkWriter object.
Declaration
public NetworkWriter()
NetworkWriter(Byte[])
Creates a new NetworkWriter object.
Declaration
public NetworkWriter(byte[] buffer)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | A buffer to write into. This is not copied. |
Properties
Position
The current position of the internal buffer.
See NetworkWriter for a code example.
Declaration
public short Position { get; }
Property Value
Type | Description |
---|---|
Int16 |
Methods
AsArray()
Returns the internal array of bytes the writer is using. This is NOT a copy.
Declaration
public byte[] AsArray()
Returns
Type | Description |
---|---|
Byte[] | Internal buffer |
FinishMessage()
This fills out the size header of a message begun with StartMessage(), so that it can be send using Send() functions.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void FinishMessage()
SeekZero()
Seeks to the start of the internal buffer.
Declaration
public void SeekZero()
StartMessage(Int16)
This begins a new message, which should be completed with FinishMessage() once the payload has been written.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void StartMessage(short msgType)
Parameters
Type | Name | Description |
---|---|---|
Int16 | msgType | Message type. |
ToArray()
Returns a copy of internal array of bytes the writer is using, it copies only the bytes used.
See NetworkWriter for a code example.
Declaration
public byte[] ToArray()
Returns
Type | Description |
---|---|
Byte[] | Copy of data used by the writer. |
Write(Boolean)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(bool value)
Parameters
Type | Name | Description |
---|---|---|
Boolean | value | The object to write. |
Write(Byte)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(byte value)
Parameters
Type | Name | Description |
---|---|---|
Byte | value | The object to write. |
Write(Byte[], Int32)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(byte[] buffer, int count)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | The byte buffer to write. |
Int32 | count | The number of bytes in the byte buffer to write. |
Write(Byte[], Int32, Int32)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(byte[] buffer, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | The byte buffer to write. |
Int32 | offset | The byte buffer array element to start writing from. |
Int32 | count | The number of bytes in the byte buffer to write. |
Write(Char)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(char value)
Parameters
Type | Name | Description |
---|---|---|
Char | value | The object to write. |
Write(Decimal)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(decimal value)
Parameters
Type | Name | Description |
---|---|---|
Decimal | value | The object to write. |
Write(Double)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(double value)
Parameters
Type | Name | Description |
---|---|---|
Double | value | The object to write. |
Write(Int16)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(short value)
Parameters
Type | Name | Description |
---|---|---|
Int16 | value | The object to write. |
Write(Int32)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(int value)
Parameters
Type | Name | Description |
---|---|---|
Int32 | value | The object to write. |
Write(Int64)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(long value)
Parameters
Type | Name | Description |
---|---|---|
Int64 | value | The object to write. |
Write(SByte)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(sbyte value)
Parameters
Type | Name | Description |
---|---|---|
SByte | value | The object to write. |
Write(Single)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(float value)
Parameters
Type | Name | Description |
---|---|---|
Single | value | The object to write. |
Write(String)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(string value)
Parameters
Type | Name | Description |
---|---|---|
String | value | The object to write. |
Write(UInt16)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(ushort value)
Parameters
Type | Name | Description |
---|---|---|
UInt16 | value | The object to write. |
Write(UInt32)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(uint value)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | value | The object to write. |
Write(UInt64)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(ulong value)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | value | The object to write. |
Write(Color)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Color value)
Parameters
Type | Name | Description |
---|---|---|
Color | value | The object to write. |
Write(Color32)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Color32 value)
Parameters
Type | Name | Description |
---|---|---|
Color32 | value | The object to write. |
Write(GameObject)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(GameObject value)
Parameters
Type | Name | Description |
---|---|---|
GameObject | value | The object to write. |
Write(Matrix4x4)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Matrix4x4 value)
Parameters
Type | Name | Description |
---|---|---|
Matrix4x4 | value | The object to write. |
Write(MessageBase)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(MessageBase msg)
Parameters
Type | Name | Description |
---|---|---|
MessageBase | msg | The network message to write. |
Write(NetworkHash128)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(NetworkHash128 value)
Parameters
Type | Name | Description |
---|---|---|
NetworkHash128 | value | The object to write. |
Write(NetworkIdentity)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(NetworkIdentity value)
Parameters
Type | Name | Description |
---|---|---|
NetworkIdentity | value | The object to write. |
Write(NetworkInstanceId)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(NetworkInstanceId value)
Parameters
Type | Name | Description |
---|---|---|
NetworkInstanceId | value | The object to write. |
Write(NetworkSceneId)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(NetworkSceneId value)
Parameters
Type | Name | Description |
---|---|---|
NetworkSceneId | value | The object to write. |
Write(Plane)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
sDeclaration
public void Write(Plane value)
Parameters
Type | Name | Description |
---|---|---|
Plane | value | The object to write. |
Write(Quaternion)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Quaternion value)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | value | The object to write. |
Write(Ray)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Ray value)
Parameters
Type | Name | Description |
---|---|---|
Ray | value | The object to write. |
Write(Rect)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Rect value)
Parameters
Type | Name | Description |
---|---|---|
Rect | value | The object to write. |
Write(Transform)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Transform value)
Parameters
Type | Name | Description |
---|---|---|
Transform | value | The object to write. |
Write(Vector2)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Vector2 value)
Parameters
Type | Name | Description |
---|---|---|
Vector2 | value | The object to write. |
Write(Vector3)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Vector3 value)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | value | The object to write. |
Write(Vector4)
This writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
using UnityEngine;
using UnityEngine.Networking;
public class ExampleScript : MonoBehaviour
{
void Start()
{
short myMsgType = 444;
NetworkWriter writer = new NetworkWriter();
writer.StartMessage(myMsgType);
writer.Write("test data");
writer.FinishMessage();
}
}
See NetworkWriter for another code example.
Declaration
public void Write(Vector4 value)
Parameters
Type | Name | Description |
---|---|---|
Vector4 | value | The object to write. |
WriteBytesAndSize(Byte[], Int32)
This writes a 16-bit count and an array of bytes of that length to the stream.
Declaration
public void WriteBytesAndSize(byte[] buffer, int count)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | Array of bytes to write. |
Int32 | count | Number of bytes from the array to write. |
WriteBytesFull(Byte[])
This writes a 16-bit count and an array of bytes of that size to the stream.
Note that this will be the full allocated size of the array. So if the array is partially filled with data to send - then you should be using WriteBytesAndSize instead.
Declaration
public void WriteBytesFull(byte[] buffer)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | Bytes to write. |
WritePackedUInt32(UInt32)
This writes the 32-bit value to the stream using variable-length-encoding.
Declaration
public void WritePackedUInt32(uint value)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | value | Value to write. |
WritePackedUInt64(UInt64)
This writes the 64-bit value to the stream using variable-length-encoding.
Declaration
public void WritePackedUInt64(ulong value)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | value | Value to write. |