Custom Spawning

Options

Hi! I've been using Photon Unity (PUN) for quite some time.

In most projects I've opted to handle the spawning of objects myself so I can use my own pooling. In PUN this was relatively straight forward, I'd just need to send over the ViewID and set it manually along with the correct owner.

Is this also manageable in Fusion or is it more complex?

Thanks,

Myles

Comments

  • OdoVR
    Options

    I would also like to know how this could be done. @myleslambert , have you figured it out?

  • Isaac_Augusto
    Options

    Hi, it's possible to do your custom pooling passing an instance of the INetworkObjectPool to your StartGameArgs.ObjectPool when starting the runner.

    The interface ask for 2 methods implementations, referring to AdquireInstance() and ReleaseInstance().

    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()

    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);
        }
      }
    

    You can use it to create your own pooling system.

    A page about it it's already being deployed and should be live soon, but i believe that the Tanknarok sample has a showcase for that.

    Hope that helps.

    -----

    Isaac Augusto

    Photon Fusion Team

  • OdoVR
    Options

    Thank you @Isaac_Augusto , I'll give it a go.

  • myleslambert
    Options

    Thanks Isaac!

    Is it possible to feed in a custom prefab id into the system? For example I already have a unique id for all spawnable prefabs for my save system and would like to reuse the same id for simplicity.

    Also it doesn't look like this will work for any sort of async prefab loading. Is that something that could be supported? Where a few frames might pass when acquiring prefab instances?

    Thanks,

    Myles