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
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ Note that the `Initialize` method is only called on non-authority clients. To cu
78
78
79
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.
80
80
81
-
#### Simple Example
81
+
#### Simple Example
82
82
83
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.
84
84
@@ -103,7 +103,7 @@ public struct InstantiateData : INetworkSerializable
103
103
}
104
104
```
105
105
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.
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.
107
107
108
108
_While there are much easier ways to synchronize the color of MeshRenderer instances across clients, this is only for example purposes._
109
109
@@ -179,10 +179,10 @@ public class SpawnWithColorHandler : NetworkPrefabInstanceHandlerWithData<Instan
179
179
/// instantiate the prefab, set the instantiation data, and then spawn.
// Assign the color to each MeshRenderer (just a psuedo example)
202
+
// Assign the color to each MeshRenderer (just a pseudo example)
203
203
renderer.material.color=instantiateData.Color;
204
204
}
205
-
206
205
returninstance.GetComponent<NetworkObject>();
207
206
}
208
207
@@ -222,7 +221,6 @@ public class SpawnWithColorHandler : NetworkPrefabInstanceHandlerWithData<Instan
222
221
```
223
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.
224
223
225
-
226
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:
227
225
228
226
- Creating a serializable structure to be serialized with the spawned network prefab.
0 commit comments