Skip to content

Commit 8696eed

Browse files
committed
fix: Serialization docs
1 parent 7c992c2 commit 8696eed

27 files changed

Lines changed: 756 additions & 373 deletions

com.unity.netcode.gameobjects/Documentation~/TableOfContents.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@
7070
* [Network update loop reference](advanced-topics/network-update-loop-system/network-update-loop-reference.md)
7171
* [Network time and ticks](advanced-topics/networktime-ticks.md)
7272
* [Serialization](serialization.md)
73+
* [Serialization overview](advanced-topics/serialization/serialization-overview.md)
7374
* [C# primitives](advanced-topics/serialization/cprimitives.md)
7475
* [Unity primitives](advanced-topics/serialization/unity-primitives.md)
7576
* [Enum types](advanced-topics/serialization/enum-types.md)
7677
* [Arrays](advanced-topics/serialization/serialization-arrays.md)
7778
* [INetworkSerializable](advanced-topics/serialization/inetworkserializable.md)
7879
* [INetworkSerializeByMemcpy](advanced-topics/serialization/inetworkserializebymemcpy.md)
79-
* [Custom serialization](advanced-topics/custom-serialization.md)
8080
* [NetworkObject serialization](advanced-topics/serialization/networkobject-serialization.md)
8181
* [FastBufferWriter and FastBufferReader](advanced-topics/fastbufferwriter-fastbufferreader.md)
82+
* [BufferSerializer](advanced-topics/bufferserializer.md)
83+
* [Custom serialization](advanced-topics/custom-serialization.md)
8284
* [Scene management](scene-management.md)
8385
* [Scene management overview](basics/scenemanagement/scene-management-overview.md)
8486
* [Integrated management](integrated-management.md)

com.unity.netcode.gameobjects/Documentation~/advanced-topics/bufferserializer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# BufferSerializer
22

3+
It's recommended to read the [serialization overview](./serialization/serialization-overview.md) before reading this documentation.
4+
35
`BufferSerializer<TReaderWriter>` is the bi-directional serializer primarily used for serializing within [`INetworkSerializable`](serialization/inetworkserializable.md) types. It wraps [`FastBufferWriter` and `FastBufferReader`](fastbufferwriter-fastbufferreader.md) to provide high performance serialization, but has a couple of differences to make it more user-friendly:
46

57
- Rather than writing separate methods for serializing and deserializing, `BufferSerializer<TReaderWriter>` allows writing a single method that can handle both operations, which reduces the possibility of a mismatch between the two
@@ -15,3 +17,7 @@ However, when those downsides are unreasonable, `BufferSerializer<TReaderWriter>
1517

1618
- For performance, you can use `PreCheck(int amount)` followed by `SerializeValuePreChecked()` to perform bounds checking for multiple fields at once.
1719
- For both performance and bandwidth usage, you can obtain the wrapped underlying reader/writer via `serializer.GetFastBufferReader()` when `serializer.IsReader` is `true`, and `serializer.GetFastBufferWriter()` when `serializer.IsWriter` is `true`. These provide micro-performance improvements by removing a level of indirection, and also give you a type you can use with `BytePacker` and `ByteUnpacker`.
20+
21+
## Serializing custom types
22+
23+
`BufferSerializer<TReaderWriter>` can be extended via extension methods to handle serializing custom types. Refer to [customizing `BufferSerializer`](./custom-serialization.md#bufferserializer) for instructions on how to do this.
Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,68 @@
11
# Custom serialization
22

3-
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:
3+
Before reading these docs, ensure you have read the [serialization overview](./serialization/serialization-overview.md)
44

5-
``
6-
Custom Types => Built In Types => INetworkSerializable
7-
``
5+
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.
86

9-
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.
7+
Let's explore different ways to implement custom serialization for a custom health struct.
108

11-
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.
9+
[!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#HealthStruct)]
1210

13-
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.
11+
## FastBufferReader and FastBufferWriter
1412

15-
### Serialize a type in a Remote Procedure Call (RPC)
13+
[`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.
1614

17-
> [!NOTE]
18-
> 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.
15+
[!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#FastBuffer)]
1916

20-
To register a custom type, or override an already handled type, you need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()`:
17+
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)
2118

22-
```csharp
23-
// Tells the Netcode how to serialize and deserialize Url in the future.
24-
// The class name doesn't matter here.
25-
public static class SerializationExtensions
26-
{
27-
public static void ReadValueSafe(this FastBufferReader reader, out Url url)
28-
{
29-
reader.ReadValueSafe(out string val);
30-
url = new Url(val);
31-
}
32-
33-
public static void WriteValueSafe(this FastBufferWriter writer, in Url url)
34-
{
35-
writer.WriteValueSafe(url.Value);
36-
}
37-
}
38-
```
19+
## BufferSerializer
3920

40-
The code generation for RPCs will automatically pick up and use these functions, and they'll become available via `FastBufferWriter` and `FastBufferReader` directly.
21+
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):
4122

42-
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:
23+
[!code-cs[](../../Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs#BufferSerializer)]
4324

44-
```csharp
45-
// The class name doesn't matter here.
46-
public static class SerializationExtensions
47-
{
48-
public static void SerializeValue<TReaderWriter>(this BufferSerializer<TReaderWriter> serializer, ref Url url) where TReaderWriter: IReaderWriter
49-
{
50-
if (serializer.IsReader)
51-
{
52-
url = new Url();
53-
}
54-
serializer.SerializeValue(ref url.Value);
55-
}
56-
}
57-
```
25+
## Remote Procedure Call (RPC)
5826

59-
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.
27+
> [!NOTE]
28+
> 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.
6029
61-
### For NetworkVariable
30+
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).
6231

63-
`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.
32+
The code generation for RPCs will automatically pick up and use these functions, as they'll become available via `FastBufferWriter` and `FastBufferReader` directly.
6433

65-
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:
34+
## NetworkVariable
6635

67-
```csharp
68-
UserNetworkVariableSerialization<Url>.WriteValue = SerializationExtensions.WriteValueSafe;
69-
UserNetworkVariableSerialization<Url>.ReadValue = SerializationExtensions.ReadValueSafe;
70-
```
36+
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.
37+
38+
First you will need to create extension methods for `FastBufferReader.ReadValueSafe()` and `FastBufferWriter.WriteValueSafe()` as [outlined above](#fastbufferreader-and-fastbufferwriter).
7139

72-
You can also use lambda expressions here:
40+
Secondly, somewhere in your application startup (before any `NetworkVariable`s using the affected types will be serialized), add the following:
7341

7442
```csharp
75-
UserNetworkVariableSerialization<Url>.WriteValue = (FastBufferWriter writer, in Url url) =>
76-
{
77-
writer.WriteValueSafe(url.Value);
78-
};
79-
80-
UserNetworkVariableSerialization<Url>.ReadValue = (FastBufferReader reader, out Url url)
81-
{
82-
reader.ReadValueSafe(out string val);
83-
url = new Url(val);
84-
};
43+
UserNetworkVariableSerialization<Health>.WriteValue = SerializationExtensions.WriteValueSafe;
44+
UserNetworkVariableSerialization<Health>.ReadValue = SerializationExtensions.ReadValueSafe;
45+
UserNetworkVariableSerialization<Health>.DuplicateValue = (in Health value, ref Health duplicatedValue) => duplicatedValue = value;
8546
```
8647

87-
When you create an extension method in `NetworkVariable<T>` you need to implement the following values:
48+
`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.
49+
50+
> [!NOTE]
51+
> `WriteValue`, `ReadValue` and `DuplicateValue` all need to be defined to customize your serialization.
52+
53+
> [!NOTE]
54+
> `WriteValue` and `ReadValue` will not be used if a type implements `INetworkSerializable` or [`INetworkSerializeByMemcpy`](./serialization/inetworkserializebymemcpy.md).
55+
56+
### Serializing delta updates
57+
58+
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:
59+
60+
- `WriteDelta`
61+
- `ReadDelta`
62+
63+
> [!NOTE]
64+
> Both `WriteDelta` and `ReadDelta` need to be defined for either to be used.
8865
89-
- `WriteValue`
90-
- `ReadValue`
91-
- `DuplicateValue`
66+
Here is a full implementation of a custom type with the methods needed for `UserNetworkVariableSerialization`
9267

93-
`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.
68+
[!code-cs[](../../Tests/Runtime/DocumentationCodeSamples/NetworkVariable/NetworkVariableSerialization.cs#HealthExample)]

com.unity.netcode.gameobjects/Documentation~/advanced-topics/fastbufferwriter-fastbufferreader.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# FastBufferWriter and FastBufferReader
22

3+
It's recommended to read the [serialization overview](./serialization/serialization-overview.md) before reading this documentation.
4+
35
The serialization and deserialization is done via `FastBufferWriter` and `FastBufferReader`. These have methods for serializing individual types and methods for serializing packed numbers, but in particular provide a high-performance method called `WriteValue()/ReadValue()` (for Writers and Readers, respectively) that can extremely quickly write an entire unmanaged struct to a buffer.
46

57
There's a trade-off of CPU usage vs bandwidth in using this: Writing individual fields is slower (especially when it includes operations on unaligned memory), but allows the buffer to be filled more efficiently, both because it avoids padding for alignment in structs, and because it allows you to use `BytePacker.WriteValuePacked()`/`ByteUnpacker.ReadValuePacked()` and `BytePacker.WriteValueBitPacked()`/`ByteUnpacker.ReadValueBitPacked()`. The difference between these two is that the BitPacked variants pack more efficiently, but they reduce the valid range of values. See the section below for details on packing.
@@ -156,3 +158,7 @@ Packing values is done using the utility classes `BytePacker` and `ByteUnpacker`
156158
| uint | 30 bits (0 to 1,073,741,824) |
157159
| long | 60 bits + sign bit (-1,152,921,504,606,846,976 to 1,152,921,504,606,846,975) |
158160
| ulong | 61 bits (0 to 2,305,843,009,213,693,952) |
161+
162+
## Serializing custom types
163+
164+
`FastBufferReader` and `FastBufferWriter` can be extended via extension methods to handle serializing custom types. Refer to [customizing `FastBufferReader` and `FastBufferWriter`](./custom-serialization.md#fastbufferreader-and-fastbufferwriter) for instructions on how to do this.

com.unity.netcode.gameobjects/Documentation~/advanced-topics/message-system/rpc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,4 @@ void Update()
328328
## Additional resources
329329

330330
* [RPC parameters](rpc-params.md)
331+
* [Customizing serialization](../custom-serialization.md#remote-procedure-call-rpc)

com.unity.netcode.gameobjects/Documentation~/advanced-topics/serialization/inetworkserializable.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# INetworkSerializable
22

3-
You can use the `INetworkSerializable` interface to define custom serializable types.
3+
It's recommended to read the [serialization overview](./serialization/serialization-overview.md) to better understand this documentation.
4+
5+
You can use the `INetworkSerializable` interface to define custom serializable types. This interface has one function: `NetworkSerialize<T>(BufferSerializer<T> serializer)`. This function is provided a bi-directional [`BufferSerializer`](../bufferserializer.md) that you can use to implement bi-directional custom serialization.
6+
7+
`INetworkSerializable` can be implemented on both unmanaged types *and* managed types. However, we recommend avoiding serializing managed types where possible to improve your game's performance.
48

59
```csharp
6-
struct MyComplexStruct : INetworkSerializable
10+
struct SpawnPoint : INetworkSerializable
711
{
812
public Vector3 Position;
913
public Quaternion Rotation;
@@ -18,24 +22,20 @@ struct MyComplexStruct : INetworkSerializable
1822
}
1923
```
2024

21-
Types implementing `INetworkSerializable` are supported by `NetworkSerializer`, `RPC` s and `NetworkVariable` s.
25+
Types implementing `INetworkSerializable` are supported by [`FastBufferReader` and `FastBufferWriter`](./fastbufferwriter-fastbufferreader.md), [`RPC`s'](../message-system/rpc.md), and [`NetworkVariable`s](../../basics/networkvariable.md).
2226

2327
```csharp
24-
2528
[Rpc(SendTo.Server)]
26-
void MyServerRpc(MyComplexStruct myStruct) { /* ... */ }
29+
void SpawnAtPointRpc(SpawnPoint spawnPoint) { /* ... */ }
2730

28-
void Update()
31+
void DoSpawnHere()
2932
{
30-
if (Input.GetKeyDown(KeyCode.P))
33+
var spawnPoint = new SpawnPoint
3134
{
32-
MyServerRpc(
33-
new MyComplexStruct
34-
{
35-
Position = transform.position,
36-
Rotation = transform.rotation
37-
}); // Client -> Server
38-
}
35+
Position = transform.position,
36+
Rotation = transform.rotation
37+
};
38+
SpawnAtPointRpc(spawnPoint); // Client -> Server
3939
}
4040
```
4141

@@ -59,7 +59,7 @@ The following example explores a more advanced use case.
5959

6060
```csharp
6161

62-
public struct MyMoveStruct : INetworkSerializable
62+
public struct SpawnWithMovement : INetworkSerializable
6363
{
6464
public Vector3 Position;
6565
public Quaternion Rotation;
@@ -125,7 +125,7 @@ Review the following example:
125125

126126
```csharp
127127

128-
public struct MyStructA : INetworkSerializable
128+
public struct SpawnPoint : INetworkSerializable
129129
{
130130
public Vector3 Position;
131131
public Quaternion Rotation;
@@ -137,17 +137,17 @@ public struct MyStructA : INetworkSerializable
137137
}
138138
}
139139

140-
public struct MyStructB : INetworkSerializable
140+
public struct SpawnInfo : INetworkSerializable
141141
{
142142
public int SomeNumber;
143143
public string SomeText;
144-
public MyStructA StructA;
144+
public SpawnPoint SpawnPoint;
145145

146146
void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
147147
{
148148
serializer.SerializeValue(ref SomeNumber);
149149
serializer.SerializeValue(ref SomeText);
150-
StructA.NetworkSerialize(serializer);
150+
SpawnPoint.NetworkSerialize(serializer);
151151
}
152152
}
153153
```
@@ -167,7 +167,7 @@ While you can have nested `INetworkSerializable` implementations (an `INetworkSe
167167

168168
```csharp
169169
/// This isn't supported.
170-
public struct MyStructB : MyStructA
170+
public struct SpawnInfo : SpawnPoint
171171
{
172172
public int SomeNumber;
173173
public string SomeText;
@@ -232,4 +232,4 @@ Then declare this network variable like so:
232232

233233
```csharp
234234
NetworkVariable<GameDataWithLong> myVar = new NetworkVariable<GameDataWithLong>();
235-
```
235+
```

0 commit comments

Comments
 (0)