Class ObjectSpawnerPool
Pre-allocates the network objects a scene will need, so a map's memory footprint is fixed at load rather than discovered under load.
public static class ObjectSpawnerPool
- Inheritance
-
ObjectSpawnerPool
- Inherited Members
Remarks
FishNet's FishNet.Utility.Performance.DefaultObjectPool already recycles despawned objects, so nothing was leaking — but it fills lazily. The first time a spawner needs a prefab it instantiates one, which means a freshly loaded map pays for every NPC it will ever show as players walk into each spawner's range: hitching during play, and a heap that only reaches its true size once the whole map has been visited. Neither is acceptable to plan capacity against.
Reserving up front converts that into a known, one-time load cost. It also removes the pathological case where a spike in concurrent spawns instantiates a batch that then sits in the pool forever: the reservation is the budget, and MaxSpawnCount caps what any spawner can draw.
Properties
TotalReserved
Total objects reserved across every prefab, for diagnostics.
public static int TotalReserved { get; }
Property Value
Methods
Clear()
Forgets all reservations. Call when tearing a scene server down so a subsequent load re-reserves against a fresh pool.
public static void Clear()
LogReservation(string)
Logs the reservation total. Called once after a scene's spawners have initialised.
public static void LogReservation(string context)
Parameters
contextstringScene or spawner description for the log line.
Reserve(NetworkManager, NetworkObject, int)
Ensures the pool holds at least count instances of a prefab.
public static int Reserve(NetworkManager networkManager, NetworkObject prefab, int count)
Parameters
networkManagerNetworkManagerThe network manager owning the pool.
prefabNetworkObjectThe prefab to reserve instances of.
countintHow many instances should exist.
Returns
- int
The number of instances newly created.
Remarks
Idempotent per prefab: reserving 5 and then 8 instantiates 5 and then 3, never 13. That matters because several spawners commonly share one prefab, and summing their maxima would multiply the reservation for no benefit — a prefab's peak concurrent count is bounded by the largest single demand plus whatever else is live, and the pool grows on demand beyond the reservation anyway.