Class NetworkVariableBaseInitializesWhenPersisted.PrefabInstanceHandler
Creates a specified number of instances that persist throughout the entire test session and will only be destroyed/released during the OneTimeTeardown
Implements
Inherited Members
Namespace: Unity.Netcode.RuntimeTests
Assembly: Unity.Netcode.RuntimeTests.dll
Syntax
public class NetworkVariableBaseInitializesWhenPersisted.PrefabInstanceHandler : INetworkPrefabInstanceHandler
Constructors
PrefabInstanceHandler(GameObject)
Declaration
public PrefabInstanceHandler(GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject |
Fields
PrefabInstances
Declaration
public Queue<GameObject> PrefabInstances
Field Value
| Type | Description |
|---|---|
| Queue<GameObject> |
Methods
AssignHandler(NetworkManager)
Invoke when NetworkManagers are created but not started.
Declaration
public static void AssignHandler(NetworkManager networkManager)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkManager | networkManager | The NetworkManager instance to assign a handler to. Must not be null and must not already have a handler assigned. |
CleanInstances()
When we are done with all tests, we finally destroy the persisted objects
Declaration
public void CleanInstances()
DeregisterHandler()
Declaration
public void DeregisterHandler()
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 void Destroy(NetworkObject networkObject)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkObject | networkObject | The NetworkObject being destroyed |
GetInstance()
Declaration
public NetworkObject GetInstance()
Returns
| Type | Description |
|---|---|
| NetworkObject |
GetInstanceToSpawn(NetworkManager)
Declaration
public static NetworkObject GetInstanceToSpawn(NetworkManager networkManager)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkManager | networkManager |
Returns
| Type | Description |
|---|---|
| NetworkObject |
Initialize(NetworkManager)
Declaration
public void Initialize(NetworkManager networkManager)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkManager | networkManager |
Instantiate(ulong, Vector3, Quaternion)
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 RegisterHostGlobalObjectIdHashValues(GameObject, List<GameObject>) 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 NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation)
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 |
Returns
| Type | Description |
|---|---|
| NetworkObject | The instantiated NetworkObject instance. Returns null if instantiation fails. |
OneTimeSetup(int, GameObject)
Declaration
public static void OneTimeSetup(int numberOfInstances, GameObject prefabInstance)
Parameters
| Type | Name | Description |
|---|---|---|
| int | numberOfInstances | |
| GameObject | prefabInstance |
ReleaseAll(bool)
Releases back to the queue and if destroy is true it will completely remove all references so they are cleaned up when
Declaration
public static void ReleaseAll(bool destroy = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | destroy | If true, completely removes all references and cleans up instances. If false, returns handlers to the queue for reuse. |
ValidateInstanceSpawnCount(int)
This validates that the instances persisted to the next test set and persisted between network sessions.
Declaration
public bool ValidateInstanceSpawnCount(int minCount)
Parameters
| Type | Name | Description |
|---|---|---|
| int | minCount | The minimum number of times each instance should have been spawned. |
Returns
| Type | Description |
|---|---|
| bool | True if all instances meet the minimum spawn count requirement or false if they do not. |
ValidatePersistedInstances(NetworkManager, int)
Validates that a NetworkManager has the minimum required number of persisted instances.
Declaration
public static bool ValidatePersistedInstances(NetworkManager networkManager, int minCount)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkManager | networkManager | The NetworkManager instance to validate. Must not be null. |
| int | minCount | The minimum number of instances that should exist. Must be greater than or equal to zero. |
Returns
| Type | Description |
|---|---|
| bool | True if the NetworkManager has at least the minimum required instances, false otherwise or if the NetworkManager is not registered. |