Photon Object Pooling

Options

Hello,

I am using Photon, for my tower defense game. In tower defense, object pooling is mandatory for performance issues. Offline game version is working great. Using classical way; when destroying a game object, first deactivating it then adding that on a pooling queue. when instantiating; getting a game object from queue and activate it. And in this method on unity profiler, there is no issue about garbage collection or memory usage/leak.

In online mode, I checked Photon guides. It seems the best practice is, shared below. Photon has a default pool inside and i am currently using it. But when played some game time, it seems memory usage increases and garbage collection make some issues when destroying and instantiating objects. Am i missing something? Or is there any chance of using a first method on Photon?

I tried some custom codes on my own, but viewId's made some trouble on that :)

Thanks for help.

DefaultPool pool = PhotonNetwork.PrefabPool as DefaultPool;
    if (pool != null && this.Prefabs != null)
    {
      foreach (GameObject prefab in this.Prefabs)
      {
        if (!pool.ResourceCache.ContainsKey(prefab.name))
          pool.ResourceCache.Add(prefab.name, prefab);
      }
    }

Answers

  • Tobias
    Options

    The DefaultPool does not actually pool anything. Look at its Destroy().

    You need to use it as "blueprint" and write your own. Likely you want to be able to limit how many objects are being pooled (cause they use memory) and maybe some objects should never pool.

    The reference docs of the interface and DefaultPool should help you coming up with a solution really quick.