Class NetworkReader | Multiplayer HLAPI | 1.0.8
docs.unity3d.com
    Show / Hide Table of Contents

    Class NetworkReader

    General 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());
           }
       }
    }
    Inheritance
    Object
    NetworkReader
    Namespace: UnityEngine.Networking
    Syntax
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class NetworkReader

    Constructors

    NetworkReader()

    Creates a new NetworkReader object.

    Declaration
    public NetworkReader()

    NetworkReader(Byte[])

    Declaration
    public NetworkReader(byte[] buffer)
    Parameters
    Type Name Description
    Byte[] buffer

    NetworkReader(NetworkWriter)

    Creates a new NetworkReader object.

    Declaration
    public NetworkReader(NetworkWriter writer)
    Parameters
    Type Name Description
    NetworkWriter writer

    A buffer to construct the reader with, this buffer is NOT copied.

    Properties

    Length

    The current length of the buffer.

    See NetworkReader for a code example.

    Declaration
    public int Length { get; }
    Property Value
    Type Description
    Int32

    Position

    The current position within the buffer.

    See NetworkReader for a code example.

    Declaration
    public uint Position { get; }
    Property Value
    Type Description
    UInt32

    Methods

    ReadBoolean()

    Reads a boolean from the stream.

    Declaration
    public bool ReadBoolean()
    Returns
    Type Description
    Boolean

    The value read.

    ReadByte()

    Reads a byte from the stream.

    Declaration
    public byte ReadByte()
    Returns
    Type Description
    Byte

    The value read.

    ReadBytes(Int32)

    Reads a number of bytes from the stream.

    See NetworkReader for a code example.

    Declaration
    public byte[] ReadBytes(int count)
    Parameters
    Type Name Description
    Int32 count

    Number of bytes to read.

    Returns
    Type Description
    Byte[]

    Bytes read. (this is a copy).

    ReadBytesAndSize()

    This read a 16-bit byte count and a array of bytes of that size from the stream.

    The format used by this function is the same as NetworkWriter.WriteBytesAndSize().

    Declaration
    public byte[] ReadBytesAndSize()
    Returns
    Type Description
    Byte[]

    The bytes read from the stream.

    ReadChar()

    Reads a char from the stream.

    Declaration
    public char ReadChar()
    Returns
    Type Description
    Char

    Value read.

    ReadColor()

    Reads a unity Color objects.

    Declaration
    public Color ReadColor()
    Returns
    Type Description
    Color

    The color read from the stream.

    ReadColor32()

    Reads a unity color32 objects.

    Declaration
    public Color32 ReadColor32()
    Returns
    Type Description
    Color32

    The color read from the stream.

    ReadDecimal()

    Reads a decimal from the stream.

    Declaration
    public decimal ReadDecimal()
    Returns
    Type Description
    Decimal

    Value read

    ReadDouble()

    Reads a double from the stream.

    Declaration
    public double ReadDouble()
    Returns
    Type Description
    Double

    Value read

    ReadGameObject()

    Reads a reference to a GameObject from the stream.

    Declaration
    public GameObject ReadGameObject()
    Returns
    Type Description
    GameObject

    The GameObject referenced.

    ReadInt16()

    Reads a signed 16 bit integer from the stream.

    Declaration
    public short ReadInt16()
    Returns
    Type Description
    Int16

    Value read

    ReadInt32()

    Reads a signed 32bit integer from the stream.

    Declaration
    public int ReadInt32()
    Returns
    Type Description
    Int32

    Value read

    ReadInt64()

    Reads a signed 64 bit integer from the stream.

    Declaration
    public long ReadInt64()
    Returns
    Type Description
    Int64

    Value read

    ReadMatrix4x4()

    Reads a unity Matrix4x4 object.

    Declaration
    public Matrix4x4 ReadMatrix4x4()
    Returns
    Type Description
    Matrix4x4

    The matrix read from the stream.

    ReadMessage<TMsg>()

    This is a utility function to read a typed network message from the stream.

    Declaration
    public TMsg ReadMessage<TMsg>()
        where TMsg : MessageBase, new()
    Returns
    Type Description
    TMsg
    Type Parameters
    Name Description
    TMsg

    The type of the Network Message, must be derived from MessageBase.

    ReadNetworkHash128()

    Reads a NetworkHash128 assetId.

    Declaration
    public NetworkHash128 ReadNetworkHash128()
    Returns
    Type Description
    NetworkHash128

    The assetId object read from the stream.

    ReadNetworkId()

    Reads a NetworkInstanceId from the stream.

    Declaration
    public NetworkInstanceId ReadNetworkId()
    Returns
    Type Description
    NetworkInstanceId

    The NetworkInstanceId read.

    ReadNetworkIdentity()

    Reads a reference to a NetworkIdentity from the stream.

    Declaration
    public NetworkIdentity ReadNetworkIdentity()
    Returns
    Type Description
    NetworkIdentity

    The NetworkIdentity object read.

    ReadPackedUInt32()

    Reads a 32-bit variable-length-encoded value.

    Declaration
    public uint ReadPackedUInt32()
    Returns
    Type Description
    UInt32

    The 32 bit value read.

    ReadPackedUInt64()

    Reads a 64-bit variable-length-encoded value.

    Declaration
    public ulong ReadPackedUInt64()
    Returns
    Type Description
    UInt64

    The 64 bit value read.

    ReadPlane()

    Reads a unity Plane object.

    Declaration
    public Plane ReadPlane()
    Returns
    Type Description
    Plane

    The plane read from the stream.

    ReadQuaternion()

    Reads a Unity Quaternion object.

    Declaration
    public Quaternion ReadQuaternion()
    Returns
    Type Description
    Quaternion

    The quaternion read from the stream.

    ReadRay()

    Reads a Unity Ray object.

    Declaration
    public Ray ReadRay()
    Returns
    Type Description
    Ray

    The ray read from the stream.

    ReadRect()

    Reads a Unity Rect object.

    Declaration
    public Rect ReadRect()
    Returns
    Type Description
    Rect

    The rect read from the stream.

    ReadSByte()

    Reads a signed byte from the stream.

    Declaration
    public sbyte ReadSByte()
    Returns
    Type Description
    SByte

    Value read

    ReadSceneId()

    Reads a NetworkSceneId from the stream.

    Declaration
    public NetworkSceneId ReadSceneId()
    Returns
    Type Description
    NetworkSceneId

    The NetworkSceneId read.

    ReadSingle()

    Reads a float from the stream.

    Declaration
    public float ReadSingle()
    Returns
    Type Description
    Single

    Value read

    ReadString()

    Reads a string from the stream. (max of 32k bytes).

    See NetworkReader for a code example.

    Declaration
    public string ReadString()
    Returns
    Type Description
    String

    Value read.

    ReadTransform()

    Reads a reference to a Transform from the stream.

    The game object of this Transform must have a NetworkIdentity.

    Declaration
    public Transform ReadTransform()
    Returns
    Type Description
    Transform

    The transform object read.

    ReadUInt16()

    Reads an unsigned 16 bit integer from the stream.

    Declaration
    public ushort ReadUInt16()
    Returns
    Type Description
    UInt16

    Value read

    ReadUInt32()

    Reads an unsigned 32 bit integer from the stream.

    Declaration
    public uint ReadUInt32()
    Returns
    Type Description
    UInt32

    Value read

    ReadUInt64()

    Reads an unsigned 64 bit integer from the stream.

    Declaration
    public ulong ReadUInt64()
    Returns
    Type Description
    UInt64

    Value read

    ReadVector2()

    Reads a Unity Vector2 object.

    Declaration
    public Vector2 ReadVector2()
    Returns
    Type Description
    Vector2

    The vector read from the stream.

    ReadVector3()

    Reads a Unity Vector3 objects.

    Declaration
    public Vector3 ReadVector3()
    Returns
    Type Description
    Vector3

    The vector read from the stream.

    ReadVector4()

    Reads a Unity Vector4 object.

    Declaration
    public Vector4 ReadVector4()
    Returns
    Type Description
    Vector4

    The vector read from the stream

    SeekZero()

    Sets the current position of the reader's stream to the start of the stream.

    Declaration
    public void SeekZero()

    ToString()

    Returns a string representation of the reader's buffer.

    Declaration
    public override string ToString()
    Returns
    Type Description
    String

    Buffer contents.

    Overrides
    Object.ToString()
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023