NetworkReader

class in UnityEngine.Networking

切换到手册

描述

用于 UNET 的通用序列化程序(用于读取字节数组)

此类与 NetworkWriter 配合使用,用于将数据序列化以便用于 UNet 命令、RPC 调用、事件和低级别消息。

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

变量

Length缓冲区的当前长度。
Position缓冲区内的当前位置。

构造函数

NetworkReader创建一个新的 NetworkReader 对象。

公共函数

ReadBoolean从流中读取一个布尔值。
ReadByte从流中读取一个字节。
ReadBytes从流中读取多个字节。
ReadBytesAndSize从流中读取一个 16 位字节和一个同样大小的字节数组。
ReadChar从流中读取一个字符。
ReadColor读取一个 Unity Color 对象。
ReadColor32读取一个 Unity color32 对象。
ReadDecimal从流中读取一个小数。
ReadDouble从流中读取一个双精度数。
ReadGameObject从流中读取一个对游戏对象的引用。
ReadInt16从流中读取一个带符号的 16 位整数。
ReadInt32从流中读取一个带符号的 32 位整数。
ReadInt64从流中读取一个带符号的 64 位整数。
ReadMatrix4x4读取一个 Unity Matrix4x4 对象。
ReadMessage一个实用函数,用于从流中读取输入的网络消息。
ReadNetworkHash128读取 NetworkHash128 assetId。
ReadNetworkId从流中读取 NetworkInstanceId。
ReadNetworkIdentity从流中读取一个对 NetworkIdentity 的引用。
ReadPackedUInt32读取一个 32 位可变长度编码值。
ReadPackedUInt64读取一个 64 位可变长度编码值。
ReadPlane读取一个 Unity Plane 对象。
ReadQuaternion读取一个 Unity Quaternion 对象。
ReadRay读取一个 Unity Ray 对象。
ReadRect读取一个 Unity Rect 对象。
ReadSByte从流中读取一个带符号字节。
ReadSceneId从流中读取一个 NetworkSceneId。
ReadSingle从流中读取一个浮点数。
ReadString从流中读取一个字符串。(最大 32k 个字节)。
ReadTransform从流中读取一个对变换组件的引用。
ReadUInt16从流中读取一个不带符号的 16 位整数。
ReadUInt32从流中读取一个不带符号的 32 位整数。
ReadUInt64从流中读取一个不带符号的 64 位整数。
ReadVector2读取一个 Unity Vector2 对象。
ReadVector3读取一个 Unity Vector3 对象。
ReadVector4读取一个 Unity Vector4 对象。
SeekZero将读取器的流的当前位置设置为流的起始位置。
ToString返回读取器缓冲区的字符串表示形式。