Custom Serializers
Custom serializers are useful where an automatic serializer may not be possible, or where you want data to be serialized in a specific manner.
When creating a custom serializer there are a few important things to remember. When you follow the proper steps your custom serializer will be found and used by Fish-Networking. Your custom serializers can also override automatic serializers, but not included ones.
Although Vector2 is already supported, the example below uses a Vector2 for simplicity sake.
// Write each axis of a Vector2.
public static void WriteVector2(this Writer writer, Vector2 value)
{
writer.WriteSingle(value.x);
writer.WriteSingle(value.y);
}
// Read and return a Vector2.
public static Vector2 ReadVector2(this Reader reader)
{
return new Vector2()
{
x = reader.ReadSingle(),
y = reader.ReadSingle()
};
}Custom serializers are more commonly used for conditional situations where what you write may change depending on the data values. Here is a more complex example where certain data is only written when it's needed.
Often when creating a custom serializer you want to use it across your entire project, and all assemblies. Without taking any action further your custom serializer would only be used on the assembly it is written. Presumably, that's probably not what you want.
But making a custom serializer work across all assemblies is very simple. Simply add the [UseGlobalCustomSerializer] attribute of the type your custom serializer is for, and done!
Example:
Last updated