Object Pooling
Last updated
Last updated
Fish-Networking has built-in functionality for Object pooling that will allow the server and client to keep instances of loaded prefabs in memory for later use. This could potentially provide better spawning performance for clients and server.
When Despawning Spawned NetworkObjects using FishNet, instead of destroying the object you may just want to disable it and store it to be used again at a later time. This describes what Object Pooling is. FishNet has a default implementation that will allow the objects you have instantiated to be disabled and pooled, instead of being destroyed. This functionality works for clients and server. You can also Pre-warm assets for later use, which is discussed later in this guide.
Reminder that Scene NetworkObjects do not get added to the Object Pool and are already disabled instead of destroyed when despawned.
As mentioned on the NetworkManager component page, there is an assignable field labeled ObjectPool. You may assign any script which inherits from the ObjectPool class. By Default when examining the NetworkManager in the Editor nothing will be assigned to this field, however when you enter play mode the NetworkManager will automatically populate it with the default implementation, and attach the script to the NetworkManager GameObject.
See below for screen captures of the NetworkManager.
By default the object pool is enabled, but your network objects will only use the pool if the default despawn behavior is modified, or through the despawn call. See below for examples to both of these.
On the NetworkObject component you can set what the default despawn behavior is for the object where the script is placed. This setting is set to "Destroy" by default, so make sure to switch this over to "Pool" if you want Fish-Networking to automatically use the default object pool.
You can manually change the despawn behavior through code for specific situations.
When using the object pool you will want to retrieve NetworkObject from it prior to network spawning them. Doing so will pull from the pool rather than instantiate new objects.
If you are certain you do not wish to use the object pool on a specific object you can still use the code above and simply set the Default Despawn Type to Destroy on the NetworkObject, or Instantiate and spawn normally.
If you want to manually store Network Objects to the ObjectPool prior to needing them at run-time you may do so through the NetworkManager API.
Here is a very basic implementation of pre-warming the ObjectPool.
FishNet allows for the user to implement their own method of object pooling. First create your own class inheriting from the ObjectPool class. Place your new class component in your scene, typically directly on the NetworkManager object. Then assign your component to the ObjectPool field on the NetworkManager.