@@ -183,22 +183,24 @@ class RemoteCallMessage : MessageBase
183
183
public uint netId ;
184
184
public int componentIndex ;
185
185
public int functionHash ;
186
- public byte [ ] payload ; // the parameters for the Cmd function
186
+ // the parameters for the Cmd function
187
+ // -> ArraySegment to avoid unnecessary allocations
188
+ public ArraySegment < byte > payload ;
187
189
188
190
public override void Deserialize ( NetworkReader reader )
189
191
{
190
192
netId = reader . ReadPackedUInt32 ( ) ;
191
193
componentIndex = ( int ) reader . ReadPackedUInt32 ( ) ;
192
194
functionHash = reader . ReadInt32 ( ) ; // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
193
- payload = reader . ReadBytesAndSize ( ) ;
195
+ payload = reader . ReadBytesAndSizeSegment ( ) ;
194
196
}
195
197
196
198
public override void Serialize ( NetworkWriter writer )
197
199
{
198
200
writer . WritePackedUInt32 ( netId ) ;
199
201
writer . WritePackedUInt32 ( ( uint ) componentIndex ) ;
200
202
writer . Write ( functionHash ) ;
201
- writer . WriteBytesAndSize ( payload ) ;
203
+ writer . WriteBytesAndSizeSegment ( payload ) ;
202
204
}
203
205
}
204
206
@@ -218,7 +220,9 @@ class SpawnPrefabMessage : MessageBase
218
220
public Vector3 position ;
219
221
public Quaternion rotation ;
220
222
public Vector3 scale ;
221
- public byte [ ] payload ;
223
+ // the serialized component data
224
+ // -> ArraySegment to avoid unnecessary allocations
225
+ public ArraySegment < byte > payload ;
222
226
223
227
public override void Deserialize ( NetworkReader reader )
224
228
{
@@ -228,7 +232,7 @@ public override void Deserialize(NetworkReader reader)
228
232
position = reader . ReadVector3 ( ) ;
229
233
rotation = reader . ReadQuaternion ( ) ;
230
234
scale = reader . ReadVector3 ( ) ;
231
- payload = reader . ReadBytesAndSize ( ) ;
235
+ payload = reader . ReadBytesAndSizeSegment ( ) ;
232
236
}
233
237
234
238
public override void Serialize ( NetworkWriter writer )
@@ -239,7 +243,7 @@ public override void Serialize(NetworkWriter writer)
239
243
writer . Write ( position ) ;
240
244
writer . Write ( rotation ) ;
241
245
writer . Write ( scale ) ;
242
- writer . WriteBytesAndSize ( payload ) ;
246
+ writer . WriteBytesAndSizeSegment ( payload ) ;
243
247
}
244
248
}
245
249
@@ -251,7 +255,9 @@ class SpawnSceneObjectMessage : MessageBase
251
255
public Vector3 position ;
252
256
public Quaternion rotation ;
253
257
public Vector3 scale ;
254
- public byte [ ] payload ;
258
+ // the serialized component data
259
+ // -> ArraySegment to avoid unnecessary allocations
260
+ public ArraySegment < byte > payload ;
255
261
256
262
public override void Deserialize ( NetworkReader reader )
257
263
{
@@ -261,7 +267,7 @@ public override void Deserialize(NetworkReader reader)
261
267
position = reader . ReadVector3 ( ) ;
262
268
rotation = reader . ReadQuaternion ( ) ;
263
269
scale = reader . ReadVector3 ( ) ;
264
- payload = reader . ReadBytesAndSize ( ) ;
270
+ payload = reader . ReadBytesAndSizeSegment ( ) ;
265
271
}
266
272
267
273
public override void Serialize ( NetworkWriter writer )
@@ -272,7 +278,7 @@ public override void Serialize(NetworkWriter writer)
272
278
writer . Write ( position ) ;
273
279
writer . Write ( rotation ) ;
274
280
writer . Write ( scale ) ;
275
- writer . WriteBytesAndSize ( payload ) ;
281
+ writer . WriteBytesAndSizeSegment ( payload ) ;
276
282
}
277
283
}
278
284
@@ -331,18 +337,20 @@ public override void Serialize(NetworkWriter writer)
331
337
class UpdateVarsMessage : MessageBase
332
338
{
333
339
public uint netId ;
334
- public byte [ ] payload ;
340
+ // the serialized component data
341
+ // -> ArraySegment to avoid unnecessary allocations
342
+ public ArraySegment < byte > payload ;
335
343
336
344
public override void Deserialize ( NetworkReader reader )
337
345
{
338
346
netId = reader . ReadPackedUInt32 ( ) ;
339
- payload = reader . ReadBytesAndSize ( ) ;
347
+ payload = reader . ReadBytesAndSizeSegment ( ) ;
340
348
}
341
349
342
350
public override void Serialize ( NetworkWriter writer )
343
351
{
344
352
writer . WritePackedUInt32 ( netId ) ;
345
- writer . WriteBytesAndSize ( payload ) ;
353
+ writer . WriteBytesAndSizeSegment ( payload ) ;
346
354
}
347
355
}
348
356
0 commit comments