Object Pooler Implementation

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on Fusion.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Object Pooler Implementation

OmerSim
2022-09-04 00:49:45

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

Comments

HippoLord
2022-09-04 08:44:33

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

Back to top