Photon Unity Networking: IPunPrefabPool Interface Reference

Photon Unity Networking

IPunPrefabPool Interface Reference

Defines all the methods that a Object Pool must implement, so that PUN can use it. More...

Public Member Functions

GameObject Instantiate (string prefabId, Vector3 position, Quaternion rotation)
 This is called when PUN wants to create a new instance of an entity prefab. Must return valid GameObject with PhotonView. More...
 
void Destroy (GameObject gameObject)
 This is called when PUN wants to destroy the instance of an entity prefab. More...
 

Detailed Description

Defines all the methods that a Object Pool must implement, so that PUN can use it.

To use a Object Pool for instantiation, you can set PhotonNetwork.ObjectPool. That is used for all objects, as long as ObjectPool is not null. The pool has to return a valid non-null GameObject when PUN calls Instantiate. Also, the position and rotation must be applied.

Please note that pooled GameObjects don't get the usual Awake and Start calls. OnEnable will be called (by your pool) but the networking values are not updated yet when that happens. OnEnable will have outdated values for PhotonView (isMine, etc.). You might have to adjust scripts.

PUN will call OnPhotonInstantiate (see IPunCallbacks). This should be used to setup the re-used object with regards to networking values / ownership.

Member Function Documentation

void IPunPrefabPool.Destroy ( GameObject  gameObject)

This is called when PUN wants to destroy the instance of an entity prefab.

A pool needs some way to find out which type of GameObject got returned via Destroy(). It could be a tag or name or anything similar.

Parameters
gameObjectThe instance to destroy.
GameObject IPunPrefabPool.Instantiate ( string  prefabId,
Vector3  position,
Quaternion  rotation 
)

This is called when PUN wants to create a new instance of an entity prefab. Must return valid GameObject with PhotonView.

Parameters
prefabIdThe id of this prefab.
positionThe position we want the instance instantiated at.
rotationThe rotation we want the instance to take.
Returns
The newly instantiated object, or null if a prefab with prefabId was not found.
Online Documentation  -  Dashboard  -  Support Forum Exit Games GmbH