Download this documentation page as a PDF.


Photon Unity Networking

A Photon Unity Networking (PUN) integration script is included with Pure Pool, to help you use object pooling in your networked game. Please follow the installation instructions for the integration script before continuing.

Integration Script Usage

The integration script takes the form of a bridge class called PrefabPool. It implements the IPunPrefabPool interface (PUN docs and PUN2 docs) required by PUN, and uses the Manager property to acquire and release objects from pools.

As an example of how to set up the bridge class, take a look at the code below:

using Umbrace.Unity.PurePool;
using Umbrace.Unity.PurePool.Photon;

using UnityEngine;

public class PunPoolingSetup : MonoBehaviour {

	private PrefabPool punPrefabPool;

	private void Start () {
		// Create the PrefabPool object, which is the bridge between PUN and Pure Pool.
		this.punPrefabPool = new PrefabPool();

		// Set the manager to load by name from the Resources folders.
		// This is required as most objects spawned by PUN will not be set up in the manager already, and will need to be loaded.
		NamedGameObjectPoolManager.Instance.UseResources = true;

		// Assign the manager, so it knows how to acquire and release from the pools.
		this.punPrefabPool.Manager = NamedGameObjectPoolManager.Instance;

		// Set PUN to use the PrefabPool object.
		PhotonNetwork.PrefabPool = punPrefabPool;


We create a new MonoBehaviour that uses the Start method to create the PrefabPool object. The Manager property is assigned to the NamedGameObjectPoolManager found in the scene (done here using the static Instance property - you can locate the manager any way you like). The PrefabPool object (punPrefabPool) is then assigned to PhotonNetwork's PrefabPool property.

With PhotonNetwork set to use the PrefabPool, all networked instantiation and destruction should now occur through Pure Pool.

PUN-specific Requirements

While OnEnable will be called when an object is taken from the pool, as well as whatever notification mode you've chosen, the networking values will not have been updated yet when that happens. Code inside OnEnable or the notification mode methods will likely have outdated values for PhotonView (isMine, etc.).

However, PUN will call OnPhotonInstantiate (see IPunCallbacks for PUN and IPunInstantiateMagicCallback for PUN2) after the values have been updated. This should be used to setup the re-used object with regards to networking values / ownership.

General Usage

Once the PunPoolingSetup component has been added to an object in your scene, you can begin using Photon Unity Networking in the same way you usually would, following the documentation for PUN.

Objects that should be networked should be spawned using PhotonNetwork.Instantiate, and destroyed using PhotonNetwork.Destroy. The bridge class in the PunPoolingSetup component will take care of redirecting the PUN requests to your NamedGameObjectPoolManager and its associated GameObjectPoolManager, so everything Photon does will be using pooled objects.

Of course, it's still important that you properly implement your notification mode to recycle and reset your objects.