2using System.Collections.Generic;
16 static readonly UTF8Encoding encoding =
new UTF8Encoding(
false,
true);
18 public static byte ReadByte(
this NetworkReader reader) => reader.ReadBlittable<
byte>();
19 public static byte? ReadByteNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
byte>();
21 public static sbyte ReadSByte(
this NetworkReader reader) => reader.ReadBlittable<sbyte>();
22 public static sbyte? ReadSByteNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<sbyte>();
25 public static char ReadChar(
this NetworkReader reader) => (char)reader.ReadBlittable<ushort>();
26 public static char? ReadCharNullable(
this NetworkReader reader) => (
char?)reader.ReadBlittableNullable<ushort>();
29 public static bool ReadBool(
this NetworkReader reader) => reader.ReadBlittable<
byte>() != 0;
30 public static bool? ReadBoolNullable(
this NetworkReader reader)
32 byte? value = reader.ReadBlittableNullable<
byte>();
33 return value.HasValue ? (value.Value != 0) :
default(
bool?);
36 public static short ReadShort(
this NetworkReader reader) => (short)reader.ReadUShort();
37 public static short? ReadShortNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
short>();
39 public static ushort ReadUShort(
this NetworkReader reader) => reader.ReadBlittable<ushort>();
40 public static ushort? ReadUShortNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<ushort>();
42 public static int ReadInt(
this NetworkReader reader) => reader.ReadBlittable<
int>();
43 public static int? ReadIntNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
int>();
45 public static uint ReadUInt(
this NetworkReader reader) => reader.ReadBlittable<uint>();
46 public static uint? ReadUIntNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<uint>();
48 public static long ReadLong(
this NetworkReader reader) => reader.ReadBlittable<
long>();
49 public static long? ReadLongNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
long>();
51 public static ulong ReadULong(
this NetworkReader reader) => reader.ReadBlittable<ulong>();
52 public static ulong? ReadULongNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<ulong>();
54 public static float ReadFloat(
this NetworkReader reader) => reader.ReadBlittable<
float>();
55 public static float? ReadFloatNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
float>();
57 public static double ReadDouble(
this NetworkReader reader) => reader.ReadBlittable<
double>();
58 public static double? ReadDoubleNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<
double>();
60 public static decimal ReadDecimal(
this NetworkReader reader) => reader.ReadBlittable<decimal>();
61 public static decimal? ReadDecimalNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<decimal>();
67 ushort size = reader.ReadUShort();
73 int realSize = size - 1;
78 throw new EndOfStreamException($
"ReadString too long: {realSize}. Limit is: {NetworkWriter.MaxStringLength}");
84 return encoding.GetString(data.Array, data.Offset, data.Count);
92 uint count = reader.ReadUInt();
94 return count == 0 ? null : reader.
ReadBytes(checked((
int)(count - 1u)));
97 public static byte[] ReadBytes(
this NetworkReader reader,
int count)
99 byte[] bytes =
new byte[count];
109 uint count = reader.ReadUInt();
111 return count == 0 ? default : reader.
ReadBytesSegment(checked((
int)(count - 1u)));
114 public static Vector2 ReadVector2(
this NetworkReader reader) => reader.ReadBlittable<Vector2>();
115 public static Vector2? ReadVector2Nullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Vector2>();
117 public static Vector3 ReadVector3(
this NetworkReader reader) => reader.ReadBlittable<Vector3>();
118 public static Vector3? ReadVector3Nullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Vector3>();
120 public static Vector4 ReadVector4(
this NetworkReader reader) => reader.ReadBlittable<Vector4>();
121 public static Vector4? ReadVector4Nullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Vector4>();
123 public static Vector2Int ReadVector2Int(
this NetworkReader reader) => reader.ReadBlittable<Vector2Int>();
124 public static Vector2Int? ReadVector2IntNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Vector2Int>();
126 public static Vector3Int ReadVector3Int(
this NetworkReader reader) => reader.ReadBlittable<Vector3Int>();
127 public static Vector3Int? ReadVector3IntNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Vector3Int>();
129 public static Color ReadColor(
this NetworkReader reader) => reader.ReadBlittable<Color>();
130 public static Color? ReadColorNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Color>();
132 public static Color32 ReadColor32(
this NetworkReader reader) => reader.ReadBlittable<Color32>();
133 public static Color32? ReadColor32Nullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Color32>();
135 public static Quaternion ReadQuaternion(
this NetworkReader reader) => reader.ReadBlittable<Quaternion>();
136 public static Quaternion? ReadQuaternionNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Quaternion>();
138 public static Rect ReadRect(
this NetworkReader reader) => reader.ReadBlittable<Rect>();
139 public static Rect? ReadRectNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Rect>();
141 public static Plane ReadPlane(
this NetworkReader reader) => reader.ReadBlittable<Plane>();
142 public static Plane? ReadPlaneNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Plane>();
144 public static Ray ReadRay(
this NetworkReader reader) => reader.ReadBlittable<Ray>();
145 public static Ray? ReadRayNullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Ray>();
147 public static Matrix4x4 ReadMatrix4x4(
this NetworkReader reader)=> reader.ReadBlittable<Matrix4x4>();
148 public static Matrix4x4? ReadMatrix4x4Nullable(
this NetworkReader reader) => reader.ReadBlittableNullable<Matrix4x4>();
151 public static Guid? ReadGuidNullable(
this NetworkReader reader) => reader.ReadBool() ? ReadGuid(reader) : default(Guid?);
155 uint netId = reader.ReadUInt();
163 return Utils.GetSpawnedInServerOrClient(netId);
166 public static NetworkBehaviour ReadNetworkBehaviour(
this NetworkReader reader)
175 uint netId = reader.ReadUInt();
181 byte componentIndex = reader.ReadByte();
187 NetworkIdentity identity = Utils.GetSpawnedInServerOrClient(netId);
189 return identity !=
null
190 ? identity.NetworkBehaviours[componentIndex]
194 public static T ReadNetworkBehaviour<T>(
this NetworkReader reader) where T : NetworkBehaviour
196 return reader.ReadNetworkBehaviour() as T;
199 public static NetworkBehaviour.NetworkBehaviourSyncVar ReadNetworkBehaviourSyncVar(
this NetworkReader reader)
201 uint netId = reader.ReadUInt();
202 byte componentIndex =
default;
207 componentIndex = reader.ReadByte();
213 public static Transform ReadTransform(
this NetworkReader reader)
216 NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
217 return networkIdentity !=
null ? networkIdentity.transform :
null;
220 public static GameObject ReadGameObject(
this NetworkReader reader)
223 NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();
224 return networkIdentity !=
null ? networkIdentity.gameObject :
null;
227 public static List<T> ReadList<T>(
this NetworkReader reader)
229 int length = reader.ReadInt();
232 List<T> result =
new List<T>(length);
233 for (
int i = 0; i < length; i++)
235 result.Add(reader.Read<T>());
240 public static T[] ReadArray<T>(
this NetworkReader reader)
242 int length = reader.ReadInt();
253 if (length > reader.Length - reader.Position)
255 throw new EndOfStreamException($
"Received array that is too large: {length}");
258 T[] result =
new T[length];
259 for (
int i = 0; i < length; i++)
261 result[i] = reader.Read<T>();
266 public static Uri ReadUri(
this NetworkReader reader)
268 string uriString = reader.ReadString();
269 return (
string.IsNullOrWhiteSpace(uriString) ?
null :
new Uri(uriString));
272 public static Texture2D ReadTexture2D(
this NetworkReader reader)
279 short width = reader.ReadShort();
280 if (width == -1)
return null;
283 short height = reader.ReadShort();
284 Texture2D texture2D =
new Texture2D(width, height);
287 Color32[] pixels = reader.ReadArray<Color32>();
288 texture2D.SetPixels32(pixels);
293 public static Sprite ReadSprite(
this NetworkReader reader)
297 Texture2D texture = reader.ReadTexture2D();
298 if (texture ==
null)
return null;
301 return Sprite.Create(texture, reader.ReadRect(), reader.ReadVector2());
NetworkIdentity identifies objects across the network.
static ArraySegment< byte > ReadBytesAndSizeSegment(this NetworkReader reader)
static byte[] ReadBytesAndSize(this NetworkReader reader)
static string ReadString(this NetworkReader reader)
Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool....
byte[] ReadBytes(byte[] bytes, int count)
Read 'count' bytes into the bytes array
ArraySegment< byte > ReadBytesSegment(int count)
Read 'count' bytes allocation-free as ArraySegment that points to the internal array.
Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool....