Skip to content

Commit 509d0fc

Browse files
committed
Docs review
1 parent 172c8af commit 509d0fc

File tree

3 files changed

+125
-124
lines changed

3 files changed

+125
-124
lines changed

com.unity.netcode.gameobjects/Documentation~/advanced-topics/network-prefab-handler.md

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# Network prefab handler
22

3-
The network prefab handler system provides advanced control over how network prefabs are instantiated and destroyed during runtime. This allows overriding the default Netcode for GameObjects [object spawning](../basics/object-spawning.md) behavior by implementing custom prefab handlers.
3+
The network prefab handler system provides advanced control over how network prefabs are instantiated and destroyed during runtime. You can use it to override the default Netcode for GameObjects [object spawning](../basics/object-spawning.md) behavior by implementing custom prefab handlers.
44

55
The network prefab handler system is accessible from the [NetworkManager](../components/networkmanager.md) as `NetworkManager.PrefabHandler`.
66

7-
## Overview
7+
## When to use a prefab handler
88

9-
For an overview of the default object spawning behavior, see the [object spawning](../basics/object-spawning.md) page. The default spawning behavior should cover the majority of spawning use cases, however there are scenarios where you may need more control:
9+
For an overview of the default object spawning behavior, refer to the [object spawning page](../basics/object-spawning.md). The default spawning behavior is designed to cover the majority of use cases, however there are some scenarios where you may need more control:
1010

1111
- **Object pooling**: Reusing objects to reduce memory allocation and initialization costs.
12-
- **Performance optimization**: Using different prefab variants on different platforms (e.g. using a simpler object for server simulation).
12+
- **Performance optimization**: Using different prefab variants on different platforms (such as using a simpler object for server simulation).
1313
- **Custom initialization**: Setting up objects with game client specific data or configurations.
1414
- **Conditional spawning**: Initializing different prefab variants based on runtime conditions.
1515

16-
The prefab handler system addresses these needs through a interface-based architecture. The system relies on two key methods, `Instantiate` and `Destroy`. `Instantiate` is called on non-authority clients when an [authority](../terms-concepts/authority.md) spawns a new [NetworkObject](../basics/networkobject.md) that has a registered network prefab handler. `Destroy` is called on all game clients whenever a registered [NetworkObject](../basics/networkobject.md) is destroyed.
16+
The prefab handler system addresses these needs through an interface-based architecture. The system relies on two key methods: `Instantiate` and `Destroy`. `Instantiate` is called on non-authority clients when an [authority](../terms-concepts/authority.md) spawns a new [NetworkObject](../basics/networkobject.md) that has a registered network prefab handler. `Destroy` is called on all game clients whenever a registered [NetworkObject](../basics/networkobject.md) is destroyed.
1717

18-
## Creating a prefab handler
18+
## Create a prefab handler
1919

20-
Prefab handlers are classes which implement on of the Netcode for GameObjects prefab handler descriptions. There are currently two such descriptions:
20+
Prefab handlers are classes that implement one of the Netcode for GameObjects prefab handler descriptions. There are currently two such descriptions:
2121

22-
- **INetworkPrefabInstanceHandler**: This is the simplest interface for custom prefab handlers.
23-
- **NetworkPrefabInstanceHandlerWithData**: This specialized handler receives custom data from the authority during spawning, enabling dynamic prefab customization.
22+
- [**INetworkPrefabInstanceHandler**](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.INetworkPrefabInstanceHandler.html): This is the simplest interface for custom prefab handlers.
23+
- [**NetworkPrefabInstanceHandlerWithData**](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkPrefabInstanceHandlerWithData.html): This specialized handler receives custom data from the authority during spawning, enabling dynamic prefab customization.
2424

25-
Netcode will use the `Instantiate` and `Destroy` methods in place of default spawn handlers for the `NetworkObject` used during spawning and de-spawning. The authority instance will use the traditional spawning approach where it will, via user script, instantiate and spawn a network prefab (even for those registered with a prefab handler). However, all non-authority clients will automatically use the instantiate method defined by the `INetworkPrefabInstanceHandler` implementation if the network prefab spawned has a registered `INetworkPrefabInstanceHandler` implementation with the `NetworkPrefabHandler` (`NetworkManager.PrefabHandler`).
25+
When using a prefab handler, Netcode for GameObjects uses the `Instantiate` and `Destroy` methods instead of default spawn handlers for the NetworkObject during spawning and despawning. The authority instance uses the traditional spawning approach where it will, via user script, instantiate and spawn a network prefab (even for those registered with a prefab handler). However, all non-authority clients will automatically use the instantiate method defined by the `INetworkPrefabInstanceHandler` implementation if the network prefab spawned has a registered `INetworkPrefabInstanceHandler` implementation with the `NetworkPrefabHandler` (`NetworkManager.PrefabHandler`).
2626

27-
### INetworkPrefabInstanceHandler
27+
### `INetworkPrefabInstanceHandler`
2828

29-
When all you want to do is handle overriding a network prefab, implementing the `INetworkPrefabInstanceHandler` interface and registering an instance of that implementation with the `NetworkPrefabHandler` (`NetworkManager.PrefabHandler`) is all you would need to do. Use the `INetworkPrefabInstanceHandler` for situations where the prefab override behavior is consistent and known.
29+
For the simple use case of overriding a network prefab, implement the `INetworkPrefabInstanceHandler` interface and register an instance of that implementation with the `NetworkPrefabHandler` (`NetworkManager.PrefabHandler`).
30+
31+
Use the `INetworkPrefabInstanceHandler` for situations where the prefab override behavior is consistent and known.
3032

3133
```csharp
3234
public interface INetworkPrefabInstanceHandler
@@ -36,9 +38,11 @@ When all you want to do is handle overriding a network prefab, implementing the
3638
}
3739
```
3840

39-
### NetworkPrefabInstanceHandlerWithData
41+
### `NetworkPrefabInstanceHandlerWithData`
42+
43+
If you want to provide additional serialized data during the instantiation process, you can derive from the `NetworkPrefabInstanceHandlerWithData` class.
4044

41-
If you are looking to provide serialized data to be used during the instantiation process, then the `NetworkPrefabInstanceHandlerWithData` class is what you would want to derive from and register with the `NetworkPrefabHandler` (`NetworkManager.PrefabHandler`). The `NetworkPrefabInstanceHandlerWithData` class allows for sending custom data from the authority during object spawning. This extra data can then be used to change the behavior of the `Instantiate` method. An implementation of `NetworkPrefabInstanceHandlerWithData` allows for sending any custom type that is serializable using [INetworkSerializable](advanced-topics/serialization/inetworkserializable.md).
45+
The `NetworkPrefabInstanceHandlerWithData` class allows you to send custom data from the authority during object spawning. This extra data can then be used to change the behavior of the `Instantiate` method. Using `NetworkPrefabInstanceHandlerWithData`, you can send any custom type that is serializable using [`INetworkSerializable`](serialization/inetworkserializable.md).
4246

4347
```csharp
4448
public abstract class NetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabInstanceHandlerWithData
@@ -49,9 +53,9 @@ public abstract class NetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabIn
4953
}
5054
```
5155

52-
## Prefab handler registration
56+
## Register a prefab handler
5357

54-
Once you have created your prefab handler (_derived class or interface implementation_), you will need to register any new instance of your prefab handler with the network prefab handler system using `NetworkManager.PrefabHandler.AddHandler`. Prefab handlers are registered against a NetworkObject's [GlobalObjectIdHash](../basics/networkobject.md#using-networkobjects).
58+
Once you've [created a prefab handler](#create-a-prefab-handler), whether by implementing or deriving, you need to register any new instance of that handler with the network prefab handler system using `NetworkManager.PrefabHandler.AddHandler`. Prefab handlers are registered against a NetworkObject's [GlobalObjectIdHash](../basics/networkobject.md#using-networkobjects).
5559

5660
```csharp
5761
public class GameManager : NetworkBehaviour
@@ -66,21 +70,21 @@ public class GameManager : NetworkBehaviour
6670
}
6771
```
6872

69-
In order to un-register a prefab handler, you can [invoke the `NetworkManager.PrefabHandler.RemoveHandler` method](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@2.4/api/Unity.Netcode.NetworkPrefabHandler.html#Unity_Netcode_NetworkPrefabHandler_RemoveHandler_System_UInt32_) (_There are several override versions of this method_).
73+
To un-register a prefab handler, you can [invoke the `NetworkManager.PrefabHandler.RemoveHandler` method](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkPrefabHandler.html#Unity_Netcode_NetworkPrefabHandler_RemoveHandler_System_UInt32_). There are several override versions of this method.
7074

7175
## Object spawning with prefab handlers
7276

73-
Once a prefab handler is registered Netcode will automatically use the defined `Initialize` and `Destroy` methods to manage the object lifecycle. [Spawn the network prefab as usual](../basics/object-spawning.md#spawning-a-network-prefab-overview) and the `Initialize` method will be called on whichever handler is registered with the spawned network prefab.
77+
Once a prefab handler is registered, Netcode for GameObjects automatically uses the defined `Initialize` and `Destroy` methods to manage the object lifecycle. [Spawn the network prefab as usual](../basics/object-spawning.md#spawning-a-network-prefab-overview) and the `Initialize` method will be called on whichever handler is registered with the spawned network prefab.
7478

7579
Note that the `Initialize` method is only called on non-authority clients. To customize network prefab behavior on the authority, you can use [prefab overrides](../basics/object-spawning.md#taking-prefab-overrides-into-consideration).
7680

7781
### Object spawning with custom data
7882

79-
When using a handler derived from `NetworkPrefabInstanceHandlerWithData`, you must manually set the instantiation data after instantiating the instance but before spawning. To do this, invoke the `NetworkPrefabInstanceHandlerWithData.SetInstantiationData` method before invoking the `NetworkObject.Spawn` method. If `SetInstantiationData` is not called, the `default` implementation will be sent to the `Instantiate` call.
83+
When using a handler derived from `NetworkPrefabInstanceHandlerWithData`, you must manually set the instantiation data after instantiating the instance (but before spawning) by invoking the `NetworkPrefabInstanceHandlerWithData.SetInstantiationData` method before invoking the `NetworkObject.Spawn` method. If `SetInstantiationData` is not called, the `default` implementation will be sent to the `Instantiate` call.
8084

81-
#### Simple Example
85+
#### Examples
8286

83-
Below we can find a simple "pseudo" example script where the `InstantiateData` structure implements the `INetworkSerializable` interface and it will be used to serialize the instantiation data for the network prefab defined within the below `SpawnPrefabWithColor` NetworkBehaviour.
87+
The first example here is a pseudo-script where the `InstantiateData` structure implements the `INetworkSerializable` interface and is used to serialize instantiation data for a network prefab defined within the `SpawnPrefabWithColor` NetworkBehaviour.
8488

8589
```csharp
8690
/// <summary>
@@ -103,9 +107,9 @@ public struct InstantiateData : INetworkSerializable
103107
}
104108
```
105109

106-
The below `SpawnPrefabWithColor` is an example of a NetworkBehavior component, to be placed on an in-scene placed NetworkObject, that handles the instantiation of a prefab handler (`SpawnWithColorHandler`) and the means to configure the network prefab to register with the handler. It also has a `SpawnWithColorSystem.SpawnObject` method that can be used to instantiate an instance of the assigned network prefab instance that will also have instantiation data associated with it that contains the color to be applied to the instance's `MeshRenderer`s.
110+
The second example, `SpawnPrefabWithColor`, is an example of a NetworkBehavior component, to be placed on an in-scene placed NetworkObject, that handles the instantiation of a prefab handler (`SpawnWithColorHandler`) and the means to configure the network prefab to register with the handler. It also has a `SpawnWithColorSystem.SpawnObject` method that can instantiate an instance of the assigned network prefab instance that will also have instantiation data associated with it that contains the color to be applied to the instance's MeshRenderers.
107111

108-
_While there are much easier ways to synchronize the color of MeshRenderer instances across clients, this is only for example purposes._
112+
There are easier ways to synchronize the color of MeshRenderer instances across clients, this is only for example purposes.
109113

110114
```csharp
111115
/// <summary>
@@ -146,13 +150,13 @@ public class SpawnPrefabWithColor : NetworkBehaviour
146150
}
147151
}
148152
```
149-
Above, we can see the `SpawnWithColorSystem` invokes the `SpawnWithColorHandler.InstantiateSetDataAndSpawn` method to create a new network prefab instance, set the instantiation data, and then spawn the network prefab instance.
153+
The `SpawnWithColorSystem` invokes the `SpawnWithColorHandler.InstantiateSetDataAndSpawn` method to create a new network prefab instance, set the instantiation data, and then spawn the network prefab instance.
150154

151-
Below we will find the slightly more complex, of the three scripts, `SpawnWithColorHandler` with a constructor that automatically registers itself and the network prefab with the `NetworkManager.PrefabHandler`:
155+
The third example is the most complex: `SpawnWithColorHandler` with a constructor that automatically registers itself and the network prefab with the `NetworkManager.PrefabHandler`:
152156

153157
```csharp
154158
/// <summary>
155-
/// The prefan instance handler that uses instantiation data to handle updating
159+
/// The prefab instance handler that uses instantiation data to handle updating
156160
/// the instance's <see cref="MeshRenderer"/>s material's color. (example purposes only)
157161
/// </summary>
158162
public class SpawnWithColorHandler : NetworkPrefabInstanceHandlerWithData<InstantiateData>
@@ -219,37 +223,36 @@ public class SpawnWithColorHandler : NetworkPrefabInstanceHandlerWithData<Instan
219223
}
220224
}
221225
```
222-
When instantiating from user script for a host, server, or distributed authority client, the above `InstantiateSetDataAndSpawn` method is used. When instantiating on non-authority instances the `GetPrefabInstance` is used since the authority provides the instantiation data.
226+
When instantiating from user script for a host, server, or distributed authority client, the above `InstantiateSetDataAndSpawn` method is used. When instantiating on non-authority instances, the `GetPrefabInstance` method is used, since the authority provides the instantiation data.
223227

224-
While setting the color of a `MeshRenderer` doesn't really provide a broad spectrum use case scenario for `NetworkPrefabInstanceHandlerWithData`, the above example does provide you with a simple implementation in order to better understand:
228+
While setting the color of a MeshRenderer doesn't really provide a broad spectrum use case scenario for `NetworkPrefabInstanceHandlerWithData`, the above example does provide you with a simple implementation to understand::
225229

226230
- Creating a serializable structure to be serialized with the spawned network prefab.
227-
- Creating a component (this case a `NetworkBehaviour`) that is used to instantiate the handler and configure the network prefab to be registered.
231+
- Creating a component (in this case a NetworkBehaviour) that's used to instantiate the handler and configure the network prefab to be registered.
228232
- Creating a `NetworkPrefabInstanceHandlerWithData` derived class that:
229233
- Handles instantiating and destroying instances of the registered prefab.
230234
- Provides an example of instantiating the network prefab, setting the instantiation data for the instance, and then spawning the instance.
231235
- Provides an example of taking the de-serialized instantiation data and using one (or more) fields to configure the network prefab instance prior to it being spawned.
232236

233-
### Pre-instantiation data vs spawn data
237+
### Pre-instantiation data and spawn data
234238

235-
While the `NetworkPrefabInstanceHandlerWithData` feature provides you with the ability to include pre-spawn instantiation data that you can then use to define the instance before the instance is instantiated and/or spawned, there are some subtle differences between pre-spawn serialized data and spawn serialized data.
239+
You can use `NetworkPrefabInstanceHandlerWithData` to include pre-spawn instantiation data that you can then use to define the instance before the instance is instantiated and/or spawned. However, the are some subtle differences between pre-spawn serialized data and spawn serialized data.
236240

237241
- *Pre-spawn serialized data*:
238-
- Is included in the `NetworkObject` serialized data.
242+
- Is included in the NetworkObject serialized data.
239243
- Is extracted and de-serialized prior to invoking the `NetworkPrefabInstanceHandlerWithData.Instantiate` method.
240244
- Can be used to identify pre-instantiated objects and/or the type of network prefab to be spawned.
241-
- Has no context for the "to-be spawned" network prefab, unless you provide that in the instantiation data.
245+
- Has no context for the network prefab that's going to be spawned, unless you provide that in the instantiation data.
242246
- Can include any kind of serialized data types supported by Netcode for GameObjects.
243247
- Can be used to spawn pre-determined instances that are created prior to connecting to the session or created while synchronizing with a session.
244248
- *Spawn serialized data*:
245-
- Is not available until the network prefab is already instantiated and is in the middle of or has finished the spawn process.
246-
- Cannot be used to define what network prefab to instantiate.
247-
- Typically will include other netcode related states (NetworkVariables, RPCs, etc.).
248-
249-
When it comes to including instantiation data you should be cautious about including data from already spawned objects. It is not that you are not allowed to do this, but that you need to assure that the serialized information of already spawned objects, like a `NetworkBehaviourReference` or `NetworkObjectReference`, exists prior to being used.
249+
- Isn't available until the network prefab is already instantiated and is in the middle of or has finished the spawn process.
250+
- Can't be used to define what network prefab to instantiate.
251+
- Typically will include other netcode related states such as NetworkVariables and RPCs.
250252

253+
When it comes to including instantiation data, you should be cautious about including data from already spawned objects. You need to ensure that the serialized information of already spawned objects, like a `NetworkBehaviourReference` or `NetworkObjectReference`, exists prior to being used.
251254

252-
## Further Reading
255+
## Additional resources
253256

254257
- [Object pooling](./object-pooling.md)
255258
- [Authority prefab overrides](../basics/object-spawning.md#taking-prefab-overrides-into-consideration)

0 commit comments

Comments
 (0)