NetworkWriter

class in UnityEngine.Networking

Switch to Manual

Description

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 out 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()); } } }

Variables

PositionThe current position of the internal buffer.

Constructors

NetworkWriterCreates a new NetworkWriter object.

Public Functions

AsArrayReturns the internal array of bytes the writer is using. This is NOT a copy.
FinishMessageThis fills out the size header of a message begun with StartMessage(), so that it can be send using Send() functions.
SeekZeroSeeks to the start of the internal buffer.
StartMessageThis begins a new message, which should be completed with FinishMessage() once the payload has been written.
ToArrayReturns a copy of internal array of bytes the writer is using, it copies only the bytes used.
WriteThis writes a reference to an object, value, buffer or network message, together with a NetworkIdentity component to the stream.
WriteBytesAndSizeThis writes a 16-bit count and an array of bytes of that length to the stream.
WriteBytesFullThis writes a 16-bit count and an array of bytes of that size to the stream.
WritePackedUInt32This writes the 32-bit value to the stream using variable-length-encoding.
WritePackedUInt64This writes the 64-bit value to the stream using variable-length-encoding.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961