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.PUNObjectFactory Class Referencesealed

Photon PUN–backed implementation of IObjectFactory. More...

Inheritance diagram for MegaCrush.Spawner.PunAdapter.PUNObjectFactory:
MegaCrush.Spawner.IObjectFactory

Public Member Functions

 PUNObjectFactory (bool masterOnly=true)
 Creates a new PUN-backed object factory.
 
ISpawnHandle Spawn (SpawnRequest r)
 Spawns a networked object via PhotonNetwork.Instantiate(string, Vector3, Quaternion, byte).
 
Task< ISpawnHandleSpawnAsync (SpawnRequest r)
 Asynchronously spawns a networked object.
 

Private Attributes

readonly bool _masterOnly
 

Detailed Description

Photon PUN–backed implementation of IObjectFactory.

This factory integrates the RuntimeSpawner system with Photon Unity Networking (PUN 2.x). It performs networked instantiation and destruction using PhotonNetwork.Instantiate(string, Vector3, Quaternion, byte) and PhotonNetwork.Destroy(UnityEngine.Object), ensuring that spawned objects replicate correctly across connected clients.

The prefab name or key must be registered with your custom IPunPrefabPool implementation (for example, PUNPoolPrefabProvider). This factory resolves the prefab key automatically using PUNPoolPrefabProvider.ResolveKey(GameObject).

When _masterOnly is enabled, only the Master Client will perform spawn operations. Other clients will automatically receive the instantiated object through PUN’s replication system.

Despawning an object uses PhotonNetwork.Destroy(UnityEngine.Object), which ensures that destruction events are also synchronized across all peers.

Constructor & Destructor Documentation

◆ PUNObjectFactory()

MegaCrush.Spawner.PunAdapter.PUNObjectFactory.PUNObjectFactory ( bool masterOnly = true)

Creates a new PUN-backed object factory.

Parameters
masterOnlyIf true, only the Master Client will perform spawn operations. If false, all clients may instantiate objects (for local simulation or testing).

Member Function Documentation

◆ Spawn()

ISpawnHandle MegaCrush.Spawner.PunAdapter.PUNObjectFactory.Spawn ( SpawnRequest r)

Spawns a networked object via PhotonNetwork.Instantiate(string, Vector3, Quaternion, byte).

Parameters
rThe spawn request containing prefab, position, rotation, and optional metadata.
Returns
A new ISpawnHandle that manages the spawned instance and its network identity, or null if spawning was refused (e.g., non-master client or invalid prefab).

This method should be called only on the authoritative client when _masterOnly is enabled.

The returned ISpawnHandle delegates destruction to PhotonNetwork.Destroy(UnityEngine.Object), which will invoke IPunPrefabPool.Destroy(GameObject) on all peers.

Implements MegaCrush.Spawner.IObjectFactory.

◆ SpawnAsync()

Task< ISpawnHandle > MegaCrush.Spawner.PunAdapter.PUNObjectFactory.SpawnAsync ( SpawnRequest r)

Asynchronously spawns a networked object.

Parameters
rThe spawn request to process.
Returns
A completed task containing the ISpawnHandle returned by Spawn(SpawnRequest).

Photon PUN performs instantiation synchronously, so this implementation simply wraps the synchronous result in a completed Task<TResult>.

Implements MegaCrush.Spawner.IObjectFactory.

Member Data Documentation

◆ _masterOnly

readonly bool MegaCrush.Spawner.PunAdapter.PUNObjectFactory._masterOnly
private

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