Object Pooler Implementation

Options

Hello,

I am having hard time to figure a very simple thing -

How to modify my single player object pooler code, into the one using Fusion's native object pooler for enabling/disabling game objects for all clients without destroying and instantiating.

I've looked into the docs for some time but it's unclear to me and it seems I'm breaking my head around a very simple manner.

I am attaching my original, non-network object pooler script, I would highly appreciate if you can help me transit this into a Fusion object pooler.

Here's the code I am using:


  protected virtual void InitializePool()

  {

    for (int i = 0; i < poolSize; i++)

    {

      GameObject obj;

        obj = Instantiate(objectToPool, this.transform);

        obj.SetActive(false);

        objectsPool.Add(obj);

    }

  }


  public virtual GameObject PoolObject()

  {

    for (int i = 0; i < objectsPool.Count; i++)

    {

      if (objectsPool[i].activeInHierarchy == false)

      {

        objectsPool[i].SetActive(true);

        return objectsPool[i];

      }

    }

Thanks in advance,

Omer

Answers

  • First, you need to create a class

    class NetworkObjectPool : INetworkObjectPool 
    {
          public NetworkObject AcquireInstance(NetworkRunner runner, NetworkPrefabInfo info) {
          if (runner.Config.PrefabTable.TryGetPrefab(info.Prefab, out var prefab)) {
            return Object.Instantiate(prefab); // replace this with the Acquire instance you have in your pool
          }
    
          return null;
        }
    
        public void ReleaseInstance(NetworkRunner runner, NetworkObject instance, bool isSceneObject) {
          Object.Destroy(instance.gameObject); // replace this with the return to pool funciton you have
        }
    }
    
    

    Then, replace the default pool that fusion already has with this one, as suggested in this link.

    https://doc.photonengine.com/en-us/fusion/current/manual/network-object/network-object-pool