Class NetworkPrefabHandler
Primary handler to add or remove customized spawn and destroy handlers for a network prefab (i.e. a prefab with a NetworkObject component)
Register custom prefab handlers by implementing the INetwork
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
public class NetworkPrefabHandler
Methods
AddHandler(uint, INetworkPrefabInstanceHandler)
Use a Global
Declaration
public bool AddHandler(uint globalObjectIdHash, INetworkPrefabInstanceHandler instanceHandler)
Parameters
Type | Name | Description |
---|---|---|
uint | globalObjectIdHash | the Global |
INetwork |
instanceHandler | a class that implements the INetwork |
Returns
Type | Description |
---|---|
bool |
AddHandler(NetworkObject, INetworkPrefabInstanceHandler)
Use a Network
Declaration
public bool AddHandler(NetworkObject prefabAssetNetworkObject, INetworkPrefabInstanceHandler instanceHandler)
Parameters
Type | Name | Description |
---|---|---|
Network |
prefabAssetNetworkObject | the Network |
INetwork |
instanceHandler | the class that implements the INetwork |
Returns
Type | Description |
---|---|
bool |
AddHandler(GameObject, INetworkPrefabInstanceHandler)
Use a Game
Declaration
public bool AddHandler(GameObject networkPrefabAsset, INetworkPrefabInstanceHandler instanceHandler)
Parameters
Type | Name | Description |
---|---|---|
Game |
networkPrefabAsset | the Game |
INetwork |
instanceHandler | class that implements the INetwork |
Returns
Type | Description |
---|---|
bool | true (registered) false (failed to register) |
AddNetworkPrefab(GameObject)
Adds a new prefab to the network prefab list. This can be any GameObject with a NetworkObject component, from any source (addressables, asset bundles, Resource.Load, dynamically created, etc)
There are three limitations to this method:
- If you have NetworkConfig.ForceSamePrefabs enabled, you can only do this before starting networking, and the server and all connected clients must all have the same exact set of prefabs added via this method before connecting
- Adding a prefab on the server does not automatically add it on the client - it's up to you to make sure the client and server are synchronized via whatever method makes sense for your game (RPCs, configurations, deterministic loading, etc)
- If the server sends a Spawn message to a client that has not yet added a prefab for, the spawn message and any other relevant messages will be held for a configurable time (default 1 second, configured via NetworkConfig.SpawnTimeout) before an error is logged. This is intended to enable the SDK to gracefully handle unexpected conditions (slow disks, slow network, etc) that slow down asset loading. This timeout should not be relied on and code shouldn't be written around it - your code should be written so that the asset is expected to be loaded before it's needed.
Declaration
public void AddNetworkPrefab(GameObject prefab)
Parameters
Type | Name | Description |
---|---|---|
Game |
prefab |
Exceptions
Type | Condition |
---|---|
Exception |
GetNetworkPrefabOverride(GameObject)
Returns the Game
Declaration
public GameObject GetNetworkPrefabOverride(GameObject gameObject)
Parameters
Type | Name | Description |
---|---|---|
Game |
gameObject | the Game |
Returns
Type | Description |
---|---|
Game |
a Game |
RegisterHostGlobalObjectIdHashValues(GameObject, List<GameObject>)
HOST ONLY!
Since a host is unique and is considered both a client and a server, for each source NetworkPrefab you must manually
register all potential Game
Declaration
public void RegisterHostGlobalObjectIdHashValues(GameObject sourceNetworkPrefab, List<GameObject> networkPrefabOverrides)
Parameters
Type | Name | Description |
---|---|---|
Game |
sourceNetworkPrefab | source NetworkPrefab to be overridden |
List<Game |
networkPrefabOverrides | one or more NetworkPrefabs could be used to override the source NetworkPrefab |
RemoveHandler(uint)
Use the Global
Declaration
public bool RemoveHandler(uint globalObjectIdHash)
Parameters
Type | Name | Description |
---|---|---|
uint | globalObjectIdHash | Global |
Returns
Type | Description |
---|---|
bool | true (success) or false (failure) |
RemoveHandler(NetworkObject)
Use the Network
Declaration
public bool RemoveHandler(NetworkObject networkObject)
Parameters
Type | Name | Description |
---|---|---|
Network |
networkObject | Network |
Returns
Type | Description |
---|---|
bool | true (success) or false (failure) |
RemoveHandler(GameObject)
Use the Game
Declaration
public bool RemoveHandler(GameObject networkPrefabAsset)
Parameters
Type | Name | Description |
---|---|---|
Game |
networkPrefabAsset | Game |
Returns
Type | Description |
---|---|
bool | true (success) or false (failure) |
RemoveNetworkPrefab(GameObject)
Remove a prefab from the prefab list. As with AddNetworkPrefab, this is specific to the client it's called on - calling it on the server does not automatically remove anything on any of the client processes.
Like AddNetworkPrefab, when NetworkConfig.ForceSamePrefabs is enabled, this cannot be called after connecting.
Declaration
public void RemoveNetworkPrefab(GameObject prefab)
Parameters
Type | Name | Description |
---|---|---|
Game |
prefab |