-
Notifications
You must be signed in to change notification settings - Fork 459
fix: Serialization docs #4007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmandM
wants to merge
5
commits into
develop-2.0.0
Choose a base branch
from
fix/serialization-docs
base: develop-2.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: Serialization docs #4007
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 43 additions & 68 deletions
111
...nity.netcode.gameobjects/Documentation~/advanced-topics/custom-serialization.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,93 +1,68 @@ | ||
| # Custom serialization | ||
|
|
||
| Netcode uses a default serialization pipeline when using `RPC`s, `NetworkVariable`s, or any other Netcode-related tasks that require serialization. The serialization pipeline looks like this: | ||
| Before reading these docs, ensure you have read the [serialization overview](./serialization/serialization-overview.md) | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| `` | ||
| Custom Types => Built In Types => INetworkSerializable | ||
| `` | ||
| Netcode for GameObjects provide support for serializing any unsupported types, and with the API provided, it can even be done with types that you haven't defined yourself, those who are behind a 3rd party wall, such as .NET types. However, the way custom serialization is implemented for RPCs and NetworkVariables is slightly different. | ||
|
|
||
| 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. | ||
| Let's explore different ways to implement custom serialization for a custom health struct. | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 has only these basic types. | ||
| [!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#HealthStruct)] | ||
|
|
||
| With this flow, you can provide support for serializing any unsupported types, and with the API provided, it can even be done with types that you haven't defined yourself, those who are behind a 3rd party wall, such as .NET types. However, the way custom serialization is implemented for RPCs and NetworkVariables is slightly different. | ||
| ## FastBufferReader and FastBufferWriter | ||
|
|
||
| ### Serialize a type in a Remote Procedure Call (RPC) | ||
| [`FastBufferReader` and `FastBufferWriter`](./fastbufferwriter-fastbufferreader.md) are the main serialization tools in Netcode for GameObjects. To register serialization for a custom type, or override an already handled type, you need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()`. Because the `FastBufferReader` and `FastBufferWriter` already know how to read and write primitive types you want to use this functionality to serialize your custom type. | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| > [!NOTE] | ||
| > From versioln 1.7.0 Remote Procedure Calls (RPCs) can also use the Network Variable flow, but NetworkVariables can't use the RPC flow. The RPC flow is more efficient when RPCs serialize the type. Unity selects the RPC flow if you implement both the RPC and Network variable flows. When a type is used by both NetworkVariables and RPCs you can use the NetworkVariable flow to lower maintenance requirements. | ||
| [!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#FastBuffer)] | ||
|
|
||
| To register a custom type, or override an already handled type, you need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()`: | ||
| Additionally, you may also need to add extensions for `FastBufferReader.ReadValue()`, `FastBufferWriter.WriteValue()` if you would like to provide for serialization without [bounds checking](./fastbufferwriter-fastbufferreader.md#bounds-checking) | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```csharp | ||
| // Tells the Netcode how to serialize and deserialize Url in the future. | ||
| // The class name doesn't matter here. | ||
| public static class SerializationExtensions | ||
| { | ||
| public static void ReadValueSafe(this FastBufferReader reader, out Url url) | ||
| { | ||
| reader.ReadValueSafe(out string val); | ||
| url = new Url(val); | ||
| } | ||
|
|
||
| public static void WriteValueSafe(this FastBufferWriter writer, in Url url) | ||
| { | ||
| writer.WriteValueSafe(url.Value); | ||
| } | ||
| } | ||
| ``` | ||
| ## BufferSerializer | ||
|
|
||
| The code generation for RPCs will automatically pick up and use these functions, and they'll become available via `FastBufferWriter` and `FastBufferReader` directly. | ||
| You can also add custom serialization support to the bi-directional [`BufferSerializer`](./bufferserializer.md). This will make this type readily available within [`INetworkSerializable`](serialization/inetworkserializable.md) types and in the [`NetworkBehaviour.OnSynchronize()` method](../components/core/networkbehaviour-synchronize.md#prespawn-synchronization-with-onsynchronize): | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 [`INetworkSerializable`](serialization/inetworkserializable.md) types: | ||
| [!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#BufferSerializer)] | ||
|
|
||
| ```csharp | ||
| // The class name doesn't matter here. | ||
| public static class SerializationExtensions | ||
| { | ||
| public static void SerializeValue<TReaderWriter>(this BufferSerializer<TReaderWriter> serializer, ref Url url) where TReaderWriter: IReaderWriter | ||
| { | ||
| if (serializer.IsReader) | ||
| { | ||
| url = new Url(); | ||
| } | ||
| serializer.SerializeValue(ref url.Value); | ||
| } | ||
| } | ||
| ``` | ||
| ## Remote Procedure Call (RPC) | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| Additionally, you can also add extensions for `FastBufferReader.ReadValue()`, `FastBufferWriter.WriteValue()`, and `BufferSerializer<TReaderWriter>.SerializeValuePreChecked()` to provide more optimal implementations for manual serialization using `FastBufferReader.TryBeginRead()`, `FastBufferWriter.TryBeginWrite()`, and `BufferSerializer<TReaderWriter>.PreCheck()`, respectively. However, none of these will be used for serializing RPCs - only `ReadValueSafe` and `WriteValueSafe` are used. | ||
| > [!NOTE] | ||
| > Remote Procedure Calls (RPCs) can also use the Network Variable flow, but NetworkVariables can't use the RPC flow. The RPC flow is more efficient when only RPCs need to serialize the type. When a type is used by both NetworkVariables and RPCs you can implement just the NetworkVariable flow to lower maintenance requirements. Unity will select the RPC flow for RPCs if you have implemented both flows. | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### For NetworkVariable | ||
| To serialize a custom type, or override an already handled type, you need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()` as [outlined above](#fastbufferreader-and-fastbufferwriter). | ||
|
|
||
| `NetworkVariable` goes through a slightly different pipeline than `RPC`s and relies on a different process for determining how to serialize its types. As a result, making a custom type available to the `RPC` pipeline doesn't automatically make it available to the `NetworkVariable` pipeline, and vice-versa. The same method can be used for both, but currently, `NetworkVariable` requires an additional runtime step to make it aware of the methods. | ||
| The code generation for RPCs will automatically pick up and use these functions, as they'll become available via `FastBufferWriter` and `FastBufferReader` directly. | ||
|
|
||
| To add custom serialization support in `NetworkVariable`, follow the steps from the "For RPCs" section to write extension methods for `FastBufferReader` and `FastBufferWriter`; then, somewhere in your application startup (before any `NetworkVariable`s using the affected types will be serialized) add the following: | ||
| ## NetworkVariable | ||
|
|
||
| ```csharp | ||
| UserNetworkVariableSerialization<Url>.WriteValue = SerializationExtensions.WriteValueSafe; | ||
| UserNetworkVariableSerialization<Url>.ReadValue = SerializationExtensions.ReadValueSafe; | ||
| ``` | ||
| Implementing [`INetworkSerializable`](./serialization/inetworkserializable.md) is the cleanest and most straightforward way to customize the serialization on a type within a [`NetworkVariable`](../basics/networkvariable.md). `UserNetworkVariableSerialization` provides runtime configuration to further override serialization of a type. | ||
|
|
||
|
EmandM marked this conversation as resolved.
Outdated
|
||
| First you will need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()` as [outlined above](#fastbufferreader-and-fastbufferwriter). | ||
|
|
||
| You can also use lambda expressions here: | ||
| Secondly, somewhere in your application startup (before any `NetworkVariable`s using the affected types will be serialized), add the following: | ||
|
|
||
| ```csharp | ||
| UserNetworkVariableSerialization<Url>.WriteValue = (FastBufferWriter writer, in Url url) => | ||
| { | ||
| writer.WriteValueSafe(url.Value); | ||
| }; | ||
|
|
||
| UserNetworkVariableSerialization<Url>.ReadValue = (FastBufferReader reader, out Url url) | ||
| { | ||
| reader.ReadValueSafe(out string val); | ||
| url = new Url(val); | ||
| }; | ||
| UserNetworkVariableSerialization<Health>.WriteValue = SerializationExtensions.WriteValueSafe; | ||
| UserNetworkVariableSerialization<Health>.ReadValue = SerializationExtensions.ReadValueSafe; | ||
| UserNetworkVariableSerialization<Health>.DuplicateValue = (in Health value, ref Health duplicatedValue) => duplicatedValue = value; | ||
| ``` | ||
|
|
||
| When you create an extension method in `NetworkVariable<T>` you need to implement the following values: | ||
| `DuplicateValue` should return a complete deep copy of the value that `NetworkVariable<T>` compares to a previous value. It is used to check whether the value has changed. `DuplicateValue` avoids re-serializing it over the network every frame when it hasn't changed. | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| > [!NOTE] | ||
| > `WriteValue`, `ReadValue` and `DuplicateValue` all need to be defined to customize your serialization. | ||
|
|
||
| > [!NOTE] | ||
| > `WriteValue` and `ReadValue` will not be used if a type implements `INetworkSerializable` or [`INetworkSerializeByMemcpy`](./serialization/inetworkserializebymemcpy.md). | ||
|
|
||
| ### Serializing delta updates | ||
|
|
||
| Simply reading and writing a value will provide the minimal amount of `NetworkVariable` functionality. This will synchronize your whole type any time any value within the type value changes. To provide sending delta updates rather than a full updates whenever your type has changed, implement the following functions: | ||
|
EmandM marked this conversation as resolved.
Outdated
|
||
|
|
||
| - `WriteDelta` | ||
| - `ReadDelta` | ||
|
|
||
| > [!NOTE] | ||
| > Both `WriteDelta` and `ReadDelta` need to be defined for either to be used. | ||
|
|
||
| - `WriteValue` | ||
| - `ReadValue` | ||
| - `DuplicateValue` | ||
| Here is a full implementation of a custom type with the methods needed for `UserNetworkVariableSerialization` | ||
|
|
||
| `DuplicateValue` returns a complete deep copy of the value that `NetworkVariable<T>` compares to a previous value to check whether or not that values has changed. This avoids reserializing it over the network every frame when it hasn't changed. | ||
| [!code-cs[](../../Tests/Runtime/DocumentationCodeSamples/NetworkVariable/NetworkVariableSerialization.cs#HealthExample)] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.