You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: com.unity.netcode.gameobjects/Documentation~/advanced-topics/network-prefab-handler.md
+42-39Lines changed: 42 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,34 @@
1
1
# Network prefab handler
2
2
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.
4
4
5
5
The network prefab handler system is accessible from the [NetworkManager](../components/networkmanager.md) as `NetworkManager.PrefabHandler`.
6
6
7
-
## Overview
7
+
## When to use a prefab handler
8
8
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:
10
10
11
11
-**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).
13
13
-**Custom initialization**: Setting up objects with game client specific data or configurations.
14
14
-**Conditional spawning**: Initializing different prefab variants based on runtime conditions.
15
15
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.
17
17
18
-
## Creating a prefab handler
18
+
## Create a prefab handler
19
19
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:
21
21
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.
24
24
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 NetworkObjectduring 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`).
26
26
27
-
### INetworkPrefabInstanceHandler
27
+
### `INetworkPrefabInstanceHandler`
28
28
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.
30
32
31
33
```csharp
32
34
publicinterfaceINetworkPrefabInstanceHandler
@@ -36,9 +38,11 @@ When all you want to do is handle overriding a network prefab, implementing the
36
38
}
37
39
```
38
40
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.
40
44
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).
@@ -49,9 +53,9 @@ public abstract class NetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabIn
49
53
}
50
54
```
51
55
52
-
## Prefab handler registration
56
+
## Register a prefab handler
53
57
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).
55
59
56
60
```csharp
57
61
publicclassGameManager : NetworkBehaviour
@@ -66,21 +70,21 @@ public class GameManager : NetworkBehaviour
66
70
}
67
71
```
68
72
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.
70
74
71
75
## Object spawning with prefab handlers
72
76
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.
74
78
75
79
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).
76
80
77
81
### Object spawning with custom data
78
82
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.
80
84
81
-
#### Simple Example
85
+
#### Examples
82
86
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.
84
88
85
89
```csharp
86
90
/// <summary>
@@ -103,9 +107,9 @@ public struct InstantiateData : INetworkSerializable
103
107
}
104
108
```
105
109
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.
107
111
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.
109
113
110
114
```csharp
111
115
/// <summary>
@@ -146,13 +150,13 @@ public class SpawnPrefabWithColor : NetworkBehaviour
146
150
}
147
151
}
148
152
```
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.
150
154
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`:
152
156
153
157
```csharp
154
158
/// <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
156
160
/// the instance's <seecref="MeshRenderer"/>s material's color. (example purposes only)
@@ -219,37 +223,36 @@ public class SpawnWithColorHandler : NetworkPrefabInstanceHandlerWithData<Instan
219
223
}
220
224
}
221
225
```
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.
223
227
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::
225
229
226
230
- 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.
228
232
- Creating a `NetworkPrefabInstanceHandlerWithData` derived class that:
229
233
- Handles instantiating and destroying instances of the registered prefab.
230
234
- Provides an example of instantiating the network prefab, setting the instantiation data for the instance, and then spawning the instance.
231
235
- 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.
232
236
233
-
### Pre-instantiation data vs spawn data
237
+
### Pre-instantiation data and spawn data
234
238
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.
236
240
237
241
-*Pre-spawn serialized data*:
238
-
- Is included in the `NetworkObject` serialized data.
242
+
- Is included in the NetworkObject serialized data.
239
243
- Is extracted and de-serialized prior to invoking the `NetworkPrefabInstanceHandlerWithData.Instantiate` method.
240
244
- 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.
242
246
- Can include any kind of serialized data types supported by Netcode for GameObjects.
243
247
- Can be used to spawn pre-determined instances that are created prior to connecting to the session or created while synchronizing with a session.
244
248
-*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.
250
252
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.
0 commit comments