class in UnityEngine.Networking
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseGeneral purpose serializer for UNET (for reading byte arrays).
This class works with NetworkWriter and is used for serializing data for UNet commands, RPC calls, events and low level messages.
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()); } } }
NetworkReader | Creates a new NetworkReader object. |
ReadBoolean | Reads a boolean from the stream. |
ReadByte | Reads a byte from the stream. |
ReadBytes | Reads a number of bytes from the stream. |
ReadBytesAndSize | This read a 16-bit byte count and a array of bytes of that size from the stream. |
ReadChar | Reads a char from the stream. |
ReadColor | Reads a unity Color objects. |
ReadColor32 | Reads a unity color32 objects. |
ReadDecimal | Reads a decimal from the stream. |
ReadDouble | Reads a double from the stream. |
ReadGameObject | Reads a reference to a GameObject from the stream. |
ReadInt16 | Reads a signed 16 bit integer from the stream. |
ReadInt32 | Reads a signed 32bit integer from the stream. |
ReadInt64 | Reads a signed 64 bit integer from the stream. |
ReadMatrix4x4 | Reads a unity Matrix4x4 object. |
ReadMessage | This is a utility function to read a typed network message from the stream. |
ReadNetworkHash128 | Reads a NetworkHash128 assetId. |
ReadNetworkId | Reads a NetworkInstanceId from the stream. |
ReadNetworkIdentity | Reads a reference to a NetworkIdentity from the stream. |
ReadPackedUInt32 | Reads a 32-bit variable-length-encoded value. |
ReadPackedUInt64 | Reads a 64-bit variable-length-encoded value. |
ReadPlane | Reads a unity Plane object. |
ReadQuaternion | Reads a Unity Quaternion object. |
ReadRay | Reads a Unity Ray object. |
ReadRect | Reads a Unity Rect object. |
ReadSByte | Reads a signed byte from the stream. |
ReadSceneId | Reads a NetworkSceneId from the stream. |
ReadSingle | Reads a float from the stream. |
ReadString | Reads a string from the stream. (max of 32k bytes). |
ReadTransform | Reads a reference to a Transform from the stream. |
ReadUInt16 | Reads an unsigned 16 bit integer from the stream. |
ReadUInt32 | Reads an unsigned 32 bit integer from the stream. |
ReadUInt64 | Reads an unsigned 64 bit integer from the stream. |
ReadVector2 | Reads a Unity Vector2 object. |
ReadVector3 | Reads a Unity Vector3 objects. |
ReadVector4 | Reads a Unity Vector4 object. |
SeekZero | Sets the current position of the reader's stream to the start of the stream. |
ToString | Returns a string representation of the reader's buffer. |
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:
Thanks for helping to make the Unity documentation better!