This document is about: FUSION 1
SWITCH TO

このページは編集中です。更新が保留になっている可能性があります。

Network Object Pool

Overview

Object pooling is a common pattern used to minimize memory fragmentation as well as lower the burden placed on the CPU and the garbage collector.

For the same reasons it is advisable, albeit not necessary, to implement a Network Object Pool to manage the pooling of NetworkObject when new ones are being acquired for a NetworkRunner.Spawn() call or released when NetworkRunner.Despawn() is run.

The object pool to be used for a Game Session needs to implement INetworkObjectPool. Furthermore, it has to be known before it is started and assigned to the StartGameArgs.ObjectPool parameter which is passed to the NetworkRunner.StartGame() method. If no object pool is specified, then the NetworkObjectPoolDefault will be used.

INetworkObjectPool

For an object pool to be compatible with the Fusion NetworkRunner, it has to implement the INetworkObjectPool interface. The interface requires the implementation of the following two methods:

  • AcquireInstance(): used to acquire an object from the pool when NetworkRunner.Spawn() has been called.
  • ReleaseInstance(): used to release and return an object from the pool whe NetworkRunner.Destroy() has been called.

By implementing the interface, it is possible to control how Fusion handles the Spawn() and Despawn() of NetworkObjects.

There can only ever be a single object pool for network objects.

NetworkObjectPoolDefault

If no INetworkPool implementation is specified, the NetworkRunner will use this internal default implementation. The default implementation does not pool objects, and instead will just creates a NetworkObject from scratch on Spawn() and destroys the instance on Despawn().

C#

class NetworkObjectPoolDefault : INetworkObjectPool {
    public NetworkObject AcquireInstance(NetworkRunner runner, NetworkPrefabInfo info) {
      if (runner.Config.PrefabTable.TryGetPrefab(info.Prefab, out var prefab)) {
        return Object.Instantiate(prefab);
      }

      return null;
    }

    public void ReleaseInstance(NetworkRunner runner, NetworkObject instance, bool isSceneObject) {
      Object.Destroy(instance.gameObject);
    }
  }
Instantiate() and Destroy() are used in this case and handled by Fusion to correctly synchronise the objects with the network simulation. Using these methods by themselves outside of this context will result local GameObjects which are not part of the network simulation.
Back to top