Class NetworkPrefabInstanceHandlerWithData<T>
Specialized version of INetworkPrefabInstanceHandler that receives custom instantiation data injected by the authority before spawning.
Implements
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
public abstract class NetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabInstanceHandler where T : struct, INetworkSerializable
Type Parameters
Name | Description |
---|---|
T | The type of the instantiation data. Must be a struct implementing INetworkSerializable. |
Remarks
Use SetInstantiationData<T>(NetworkObject, T) or SetInstantiationData<T>(GameObject, T) on the authority side to set instantiation data before spawning an object or synchronizing a client. The data set on the authority will then be passed into the Instantiate(ulong, Vector3, Quaternion, T) call.
Methods
Destroy(NetworkObject)
Invoked on Client and Server Once an implementation is registered with the NetworkPrefabHandler, this method will be called when a Network Prefab associated NetworkObject is:
Server Side: destroyed or despawned with the destroy parameter equal to true If Despawn(bool) is invoked with the default destroy parameter (i.e. false) then this method will NOT be invoked!
Client Side: destroyed when the client receives a destroy object message from the server or host.
Note on Pooling: When this method is invoked, you do not need to destroy the NetworkObject as long as you want your pool to persist. The most common approach is to make the NetworkObject inactive by calling SetActive(bool).
Declaration
public abstract void Destroy(NetworkObject networkObject)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | networkObject | The NetworkObject being destroyed |
Instantiate(ulong, Vector3, Quaternion, T)
Client Side Only Once an implementation is registered with the NetworkPrefabHandler, this method will be called every time a Network Prefab associated NetworkObject is spawned on clients
Note On Hosts: Use the NetworkPrefabHandler.RegisterHostGlobalObjectIdHashValues(GameObject, List<T>) method to register all targeted NetworkPrefab overrides manually since the host will be acting as both a server and client.
Note on Pooling: If you are using a NetworkObject pool, don't forget to make the NetworkObject active via the SetActive(bool) method.
Declaration
public abstract NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, T instantiationData)
Parameters
Type | Name | Description |
---|---|---|
ulong | ownerClientId | the owner for the NetworkObject to be instantiated |
Vector3 | position | the initial/default position for the NetworkObject to be instantiated |
Quaternion | rotation | the initial/default rotation for the NetworkObject to be instantiated |
T | instantiationData | Custom data of type |
Returns
Type | Description |
---|---|
NetworkObject | The instantiated NetworkObject instance. Returns null if instantiation fails. |
Remarks
If you need to pass custom data at instantiation time (e.g., selecting a variant, setting initialization parameters, or choosing a pre-instantiated object), implement NetworkPrefabInstanceHandlerWithData<T> instead.