NetworkReader/Writer
Usage
For example, this is how we serialize an int
and a float3
position in a message:
Reading and Writing is:
Atomic:
WriteInt
either writes all 4 bytesint
, or none if there is not enough space.ReadInt
either reads all 4 bytesint
, or none if the buffer doesn’t have enough data.Allocation Free: all reads and writes are allocation free for maximum performance.
Fast: the Bitpacker’s internal buffer writes are always word aligned. This makes unaligned writes (
byte
/ushort
/etc.) as fast as aligned writes (uint
,float
, etc.).
Reference Passing
To avoid allocations, NetworkReader/Writer are value types (structs). When passing a reader/writer through functions, make sure to pass it as reference:
If you don’t pass it as reference, then it will create a copy of the Writer/Reader, which would not modify the original writer/reader’s Position.
In other words, always pass NetworkReader/Writer as reference!
Burst
DOTSNET NetworkReader/Writers are burstable.
Additionally, DOTSNET provides fixed size Readers/Writers for use in IComponentData
:
NetworkReader128
NetworkWriter128
Extending NetworkReader/Writer
You can extend NetworkReader/Writer with C#’s extension system:
Last updated