Network Prefab Pool
Object pooling is a very effective technique to avoid run-time allocations (and thus, improve performance), by creating a pool of objects of the same type, at the start of the game. So that when you want to instantiate a certain prefab, you will not create a new object in memory. But rather, all instances of that prefab are already created, and you simply grab one out of the pool and initialize it. And when you want to destroy an instance, instead of removing it from memory (which causes GC), you put it back on the pool โ recycling it.
Pooling is extremely useful and effective if you have a prefab in your game that you instantiate and destroy repeatedly. For instance, the bomb in Bomberman.
Netick has a built-in pooling system that you can use.
By default, all prefabs are not pooled. To enable pooling for a certain prefab, you must call InitializePool
(must be called at the start of Netick in NetworkEventsListner) on that prefab and pass it the initial amount to create:
And if this amount happens to be exceeded, Netick will simply create more objects in the pool automatically.
And you donโt need to use special instantiate or destroy methods to deal with pooled prefabs, it all works through the same Sandbox.NetworkInstantiate
and Sandbox.Destroy
methods.
Although you still need to reset your objects. However, Netick automatically resets all network properties to their declaration values.
Resetting Prefab Instances
To reset your object, override NetworkReset on your class inheriting from NetworkBehaviour:
Last updated
Was this helpful?