Photon PUN 2+ Integration Guide
The Runtime Spawner package includes optional support for Photon PUN 2+, enabling fully networked spawning, pooling, and synchronization in cooperative or multiplayer games.
This guide explains how to connect the Runtime Spawner to Photon PUN 2+, using the included sample scene and bootstrap scripts.
Requirements
Unity
2022.3 or newer
Photon PUN 2 or PUN 2+
Imported from the Asset Store
Megacrush Runtime Spawner
v 1.5.0 or newer
Megacrush Object Pool
Installed automatically with Runtime Spawner
1. Import Samples
Runtime Spawner provides several sample scenes. To get started with PUN integration:
In Package Manager → Runtime Spawner → Samples, import:
Runtime Spawner Sample
Runtime Spawner PUN Sample
The PUN sample includes an example PunBootstrap script showing how to:
Connect to Photon
Register prefabs in the pool
Start the Runtime Spawner on the Master Client
Use this as a reference for your own network bootstrap logic.
2. Scene Setup
Create a new scene or open the PUN Sample Scene.
Add an Empty GameObject named
PunBootstrap.Attach
PunBootstrap.cs(Assets/Samples/Runtime Spawner/1.5.0/Runtime Spawner PUN Sample/Code/Runtime/Bootstrap/PunBootstrap.cs).
Inspector Fields
Spawner
Optional reference to your RuntimeSpawner in-scene. Runs only on the Master Client.
Player Prefab
Prefab with a PhotonView and optional controller script.
PUN Prefabs to Register
List of other prefabs used by the spawner (enemies, pickups, etc.).
Prewarm Each
Number of pooled instances per prefab to create at startup.
Player Spawn Point
Optional Transform for initial player placement.
VCam
Optional Cinemachine Virtual Camera to follow the local player.
UI Fields
TMP Input, Buttons, and Status Text for the sample UI.
3. Player Prefab Setup
Each networked prefab must have a PhotonView.
Add a
PhotonViewcomponent.(Optional) Add a
PUNPrefabKeycomponent and assign a unique ID string.If no ID is set, the prefab name is used as its key.
Place the prefab anywhere under
Assets/Prefabs/or another included folder - no need forResources.
The Runtime Spawner’s PUN provider handles lookup and registration automatically.
4. Prefab Registration with the Provider
PunBootstrap registers all relevant prefabs at runtime:
Registers the Player Prefab and all Prefabs to Register.
Auto-registers prefabs referenced by
RuntimeSpawner,LocalAreaSpawner,WaveSpawner, andSpecialEncounterManagerviaPUNPrefabRegistrar.RegisterFromScene(_provider);
If you add new prefabs at runtime or after a scene change, refresh manually:
_provider.Clear();
PUNPrefabRegistrar.RegisterFromScene(_provider, prewarmEach);5. Running the Sample
Enter Play mode.
In the UI:
Type a Room Name (or use the default).
Click Connect / Start.
The bootstrap will:
Connect to Photon Cloud.
Join or create the room.
Register all prefabs in the pool.
Spawn the local player.
Start the Runtime Spawner on the Master Client.
You’ll see logs like:
[PunBootstrap] Joined room as Master
[PunBootstrap] Spawner started (Master Client)6. Multiplayer Testing
To verify networked spawning:
Run one instance (Host) from the Editor.
Build and launch a second (Client) instance.
Both join the same room.
The first client is the Master Client and runs the Runtime Spawner.
Additional clients spawn their player objects via Photon instantiation.
When the Master Client leaves, Photon elects a new master; the new master automatically restarts the spawner.
7. Common Pitfalls and Fixes
Prefab not found
Not registered with provider
Add prefab to PUN Prefabs to Register or reference it in scene spawner
“Has no PhotonView” warning
Prefab missing PhotonView
Add a PhotonView component; verify it gets a View ID at runtime
Double spawners running
Bootstrap not restricted to Master Client
Keep PunBootstrap.masterOnly = true
Missing pools
Object Pool package not imported
Import Megacrush Object Pool from Package Manager
No spawn activity in room
Spawner never started on master
Ensure PunBootstrap calls spawner.Init() and spawner.StartSpawners() after joining room
8. Extending the Integration
For custom projects, you can:
Replace
PunBootstrapwith your own connection manager.Use
PUNPoolPrefabProviderdirectly and register prefabs manually.Integrate the registration and spawner start/stop calls into your existing Photon game flow.
Example snippet:
if (PhotonNetwork.IsMasterClient)
{
spawner.Init();
spawner.StartSpawners();
}9. Quick Checklist
✅ Player Prefab has a PhotonView (and optional
PUNPrefabKey)✅ All networked prefabs are registered in
PUNPoolPrefabProvider✅
PunBootstrapin scene with UI + Spawner wired up✅ Megacrush Object Pool imported
✅ Press Play → Connect → Spawn → Master Client runs spawner
10. Integration Notes
Only the Master Client runs the Runtime Spawner; other clients observe replicated objects.
Pooling is handled via
PUNPoolPrefabProvider, bridging Photon instantiation and the Runtime Spawner’s object pool.Lifecycle control (
Pause,Resume,End) is automatically supported through the Spawner API, allowing safe room transitions or disconnects.Works alongside the Fusion Integration – choose either based on your network stack.
✅ You’re Ready to Go
Once your prefabs are registered and the PunBootstrap is configured,
the Runtime Spawner handles all networked spawn and despawn activity through Photon PUN 2+, automatically synchronizing players, AI, and encounter systems across all clients.
Use the included PUN Sample Scene as a template for your own multiplayer project.
Last updated