Runtime Spawner 1.5.1
Generic Runtime spawn and instance pooling system for spawning random AI agents around a map. Works with ANY AI system easily.
Loading...
Searching...
No Matches
MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider Class Referencesealed

Custom IPunPrefabPool implementation that integrates Photon Unity Networking (PUN) with the RuntimeSpawner and project-wide object pool. More...

Inheritance diagram for MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider:

Public Member Functions

 PUNPoolPrefabProvider (IPoolAdapter pool)
 Creates a new PUN pool provider using the given IPoolAdapter.
 
void Register (string key, GameObject prefab, int prewarmCount=0)
 Registers a prefab for use with PhotonNetwork instantiation.
 
void RegisterAll (IEnumerable<(string key, GameObject prefab, int prewarm)> entries)
 Registers multiple prefabs in bulk.
 
bool TryResolvePrefab (string key, out GameObject prefab)
 Attempts to resolve a prefab asset by its registered key.
 
bool TryResolveKey (GameObject prefab, out string key)
 Attempts to resolve the registered key for a given prefab asset.
 
void LogRegisteredKeys ()
 Logs all registered prefab keys and their associated prefab names.
 
GameObject Instantiate (string prefabId, Vector3 position, Quaternion rotation)
 Instantiates a prefab using the object pool instead of new GameObject().
 
void Destroy (GameObject gameObject)
 Returns a GameObject instance to the pool instead of destroying it.
 
void Clear ()
 Clears all registered prefab mappings.
 

Static Public Member Functions

static string ResolveKey (GameObject prefab)
 Resolves the preferred key for a prefab.
 

Private Attributes

readonly IPoolAdapter _pool
 
readonly Dictionary< string, GameObject > _keyToPrefab = new()
 
readonly Dictionary< GameObject, string > _prefabToKey = new()
 
bool logToConsole = false
 

Detailed Description

Custom IPunPrefabPool implementation that integrates Photon Unity Networking (PUN) with the RuntimeSpawner and project-wide object pool.

This provider allows PhotonNetwork to create and destroy networked objects using your existing pooling system instead of Unity’s Instantiate/Destroy. It ensures that networked prefabs are spawned and recycled efficiently while remaining compatible with PhotonNetwork.Instantiate(string, Vector3, Quaternion, byte).

Prefabs are registered with unique string keys—either manually via Register(string, GameObject, int), or automatically using PUNPrefabKey components attached to prefab assets.

This class maintains a bidirectional map between prefab keys and prefab instances, allowing PhotonNetwork.Instantiate(string, Vector3, Quaternion, byte) to resolve prefabs at runtime and ensuring they can be correctly returned to the pool on destroy.

To enable this provider, assign it to PhotonNetwork.PrefabPool at application startup:

PhotonNetwork.PrefabPool = new PUNPoolPrefabProvider(new PoolAdapter());
Default adapter that forwards calls to PoolManager from MegaCrush.ObjectPool.
Definition PoolAdapter.cs:42
Custom IPunPrefabPool implementation that integrates Photon Unity Networking (PUN) with the RuntimeSp...
Definition PUNPoolPrefabProvider.cs:36

Constructor & Destructor Documentation

◆ PUNPoolPrefabProvider()

MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.PUNPoolPrefabProvider ( IPoolAdapter pool)

Creates a new PUN pool provider using the given IPoolAdapter.

Parameters
poolThe project-wide object pool adapter responsible for managing pooled instances.

Member Function Documentation

◆ Clear()

void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Clear ( )

Clears all registered prefab mappings.

Useful when leaving a room or switching scenes to prevent stale prefab references between sessions.

◆ Destroy()

void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Destroy ( GameObject gameObject)

Returns a GameObject instance to the pool instead of destroying it.

Parameters
gameObjectThe GameObject to recycle.

PhotonNetwork calls this automatically when a networked object is destroyed.

◆ Instantiate()

GameObject MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Instantiate ( string prefabId,
Vector3 position,
Quaternion rotation )

Instantiates a prefab using the object pool instead of new GameObject().

Parameters
prefabIdThe registered prefab key requested by PhotonNetwork.
positionThe spawn position in world space.
rotationThe spawn rotation in world space.
Returns
The instantiated GameObject (inactive by default).

PhotonNetwork will call this automatically when spawning a prefab whose key matches prefabId .

Instances are returned inactive to match PUN’s expected lifecycle.

◆ LogRegisteredKeys()

void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.LogRegisteredKeys ( )

Logs all registered prefab keys and their associated prefab names.

◆ Register()

void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Register ( string key,
GameObject prefab,
int prewarmCount = 0 )

Registers a prefab for use with PhotonNetwork instantiation.

Parameters
keyThe string key that identifies this prefab in PhotonNetwork.
prefabThe prefab GameObject to associate with this key.
prewarmCountOptional number of instances to preallocate in the pool.

If a mapping for key already exists, it will be replaced.

◆ RegisterAll()

void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.RegisterAll ( IEnumerable<(string key, GameObject prefab, int prewarm)> entries)

Registers multiple prefabs in bulk.

Parameters
entriesA collection of key/prefab/prewarmCount tuples to register.

◆ ResolveKey()

static string MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.ResolveKey ( GameObject prefab)
static

Resolves the preferred key for a prefab.

Parameters
prefabThe prefab to resolve.
Returns
The PUNPrefabKey.Id value if present, otherwise the prefab’s GameObject.name.

◆ TryResolveKey()

bool MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.TryResolveKey ( GameObject prefab,
out string key )

Attempts to resolve the registered key for a given prefab asset.

Parameters
prefabThe prefab to look up.
keyThe registered key if found.
Returns
true if a key exists; otherwise, false.

◆ TryResolvePrefab()

bool MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.TryResolvePrefab ( string key,
out GameObject prefab )

Attempts to resolve a prefab asset by its registered key.

Parameters
keyThe prefab key as registered in this provider.
prefabThe corresponding prefab if found.
Returns
true if a prefab was found; otherwise, false.

Member Data Documentation

◆ _keyToPrefab

readonly Dictionary<string, GameObject> MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider._keyToPrefab = new()
private

◆ _pool

readonly IPoolAdapter MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider._pool
private

◆ _prefabToKey

readonly Dictionary<GameObject, string> MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider._prefabToKey = new()
private

◆ logToConsole

bool MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.logToConsole = false
private

The documentation for this class was generated from the following file: