id: custom-serialization title: Custom Serialization
NetworkVariable's or any other Netcode for GameObjects (Netcode) related task that requires serialization. The Netcode uses a default serialization pipeline that looks like this:
Custom Types => Built In Types => INetworkSerializable
That is, when Netcode first gets hold of a type, it will check for any custom types that the user have registered for serialization, after that it will check if it's a built in type, such as a Vector3, float etc. These are handled by default. If not, it will check if the type inherits
INetworkSerializable, if it does, it will call it's write methods.
By default, any type that satisfies the
unmanaged generic constraint can be automatically serialized as RPC parameters. This includes all basic types (bool, byte, int, float, enum, etc) as well as any structs that contains only these basic types.
With this flow, you can override ALL serialization for ALL types, even built in types, and with the API provided, it can even be done with types that you have not defined yourself, those who are behind a 3rd party wall, such as .NET types.
To register a custom type, or override an already handled type, you need to create extension methods for
The code generation for RPCs will automatically pick up and use these functions, and they'll become available via
You can also optionally use the same method to add support for
BufferSerializer<TReaderWriter>.SerializeValue(), if you wish, which will make this type readily available within
Additionally, you can also add extensions for
BufferSerializer<TReaderWriter>.SerializeValuePreChecked() to provide more optimal implementations for manual serialization using
BufferSerializer<TReaderWriter>.PreCheck(), respectively. However, none of these will be used for serializing RPCs - only
WriteValueSafe are used.