PurrNet auto networks structs, classes and interfaces for you, given they directly or indirectly consist of serializable data.
Networking custom classes and structs can be extremely useful to send over the network, for example by using basic RPC's.
IPackedAuto
This will allow PurrNet to automatically handle the serialization for you. This is useful for Broadcasting, as data outside of a Network Behaviour may have trouble serializing over the network.
An example of using this is below:
This allows you to handle the serialization yourself. This is the easiest way if you need to make your own serializer.
An example of using this is below:
This also allows you to handle the serialization yourself. Contrary to the previous method, this allows you to disconnect the reading and writing of data, for more special cases.
An example of using this is below:
This is the most performant way of handling custom reading and writing of your data. The PurrNet serialization system will automatically find your static type.
Below is an example of using this with a Vector2:
publicstatic CustomSerializerForExternalType{ public staticvoidWrite(thisBitPacker packer,Vector2 value) {packer.Write(value.x);packer.Write(value.y); } public staticvoidRead(thisBitPacker packer,refVector2 value) {packer.Read(ref value.x);packer.Read(ref value.y); }}
Packing directly RPC
You can pack data directly and optimally using the BitPackerPool and simply sending the BitPacker directly through an RPC. This uses less memory and allows you to easily pack custom data on the fly.
protectedoverridevoidOnSpawned(bool asServer){if (!asServer) {usingvar writer =BitPackerPool.Get();Packer<string>.Write(writer,"Hello, server!");Packer<string>.Write(writer,"Maybe some audio data being pumped?");MyRPC(writer); }}[ServerRpc(requireOwnership:false)]privatevoidMyRPC(BitPacker data){using (data) {string message =default;string audioData =default;Packer<string>.Read(data,ref message);Packer<string>.Read(data,ref audioData);PurrLogger.Log(message); // Hello, server!PurrLogger.Log(audioData); // Maybe some audio data being pumped? }}