Skip to main content

Object Pooling

Netcode for GameObjects (Netcode) provides built-in support for Object Pooling, which allows you to override the default Netcode destroy and spawn handlers with your own logic. This allows you to store destroyed network objects in a pool to reuse later. This is useful for frequently used objects, such as projectiles, and is a way to increase the application's overall performance. By pre-instantiating and reusing the instances of those objects, object pooling removes the need to create or destroy objects at runtime, which could save a lot of work for the CPU. This means that instead of creating or destroying the same object over and over again, it is simply deactivated after use, then, when another object is needed, the pool recycles one of the deactivated objects and reactivates it.

See Introduction to Object Pooling to learn more about the importance of pooling objects.

NetworkPrefabInstanceHandler#

You can register your own spawn handlers by including the INetworkPrefabInstanceHandler interface and registering with the NetworkPrefabHandler.

public interface INetworkPrefabInstanceHandler
{
NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation);
void Destroy(NetworkObject networkObject);
}

Netcode will use the Instantiate and Destroy methods in place of default spawn handlers for the NetworkObject used during spawning and despawning. Because the message to instantiate a new NetworkObject originates from a Host or Server, both will not have the Instantiate method invoked. All clients (excluding a Host) will have the instantiate method invoked if the INetworkPrefabInstanceHandler implementation is registered with NetworkPrefabHanlder (NetworkManager.PrefabHandler) and a Host or Server spawns the registered/associated NetworkObject.

The following example is from the Bossroom Sample. It shows how object pooling is used to handle the different projectile objects. In that example, the class NetworkObjectPool is the datastructure containing the pooled objects and the class PooledPrefabInstanceHandler is the handler implementing INetworkPrefabInstanceHandler.

Assets/BossRoom/Scripts/Shared/Net/NetworkObjectPool.cs
loading...
See full example on GitHub

Let's have a look at NetworkObjectPool first. PooledPrefabsList contains a list of prefabs to handle, with an initial number of instances to spawn for each. In the Start method, it initialises the different pools for each prefab as queues inside the pooledObjects dictionary. It also instantiates the handlers for each prefab and registers them. To use these objects, a user then needs to obtain it via the GetNetworkObject method before spawning it, then return the object to the pool after use with ReturnNetworkObject before despawning it. This only needs to be done on the server, as the PooledPrefabInstanceHandler will handle it on the client(s) when the network object's Spawn or Despawn method is called, via its Instantiate and Destroy methods. Inside those methods, the PooledPrefabInstanceHandler simply calls the pool to get the corresponding object, or to return it.