|
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.
|
Custom IPunPrefabPool implementation that integrates Photon Unity Networking (PUN) with the RuntimeSpawner and project-wide object pool. More...
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 |
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:
| MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.PUNPoolPrefabProvider | ( | IPoolAdapter | pool | ) |
Creates a new PUN pool provider using the given IPoolAdapter.
| pool | The project-wide object pool adapter responsible for managing pooled instances. |
| 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.
| void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Destroy | ( | GameObject | gameObject | ) |
Returns a GameObject instance to the pool instead of destroying it.
| gameObject | The GameObject to recycle. |
PhotonNetwork calls this automatically when a networked object is destroyed.
| GameObject MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Instantiate | ( | string | prefabId, |
| Vector3 | position, | ||
| Quaternion | rotation ) |
Instantiates a prefab using the object pool instead of new GameObject().
| prefabId | The registered prefab key requested by PhotonNetwork. |
| position | The spawn position in world space. |
| rotation | The spawn rotation in world space. |
PhotonNetwork will call this automatically when spawning a prefab whose key matches prefabId .
Instances are returned inactive to match PUN’s expected lifecycle.
| void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.LogRegisteredKeys | ( | ) |
Logs all registered prefab keys and their associated prefab names.
| void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.Register | ( | string | key, |
| GameObject | prefab, | ||
| int | prewarmCount = 0 ) |
Registers a prefab for use with PhotonNetwork instantiation.
| key | The string key that identifies this prefab in PhotonNetwork. |
| prefab | The prefab GameObject to associate with this key. |
| prewarmCount | Optional number of instances to preallocate in the pool. |
If a mapping for key already exists, it will be replaced.
| void MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.RegisterAll | ( | IEnumerable<(string key, GameObject prefab, int prewarm)> | entries | ) |
Registers multiple prefabs in bulk.
| entries | A collection of key/prefab/prewarmCount tuples to register. |
|
static |
Resolves the preferred key for a prefab.
| prefab | The prefab to resolve. |
| bool MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.TryResolveKey | ( | GameObject | prefab, |
| out string | key ) |
Attempts to resolve the registered key for a given prefab asset.
| prefab | The prefab to look up. |
| key | The registered key if found. |
true if a key exists; otherwise, false.| bool MegaCrush.Spawner.PunAdapter.PUNPoolPrefabProvider.TryResolvePrefab | ( | string | key, |
| out GameObject | prefab ) |
Attempts to resolve a prefab asset by its registered key.
| key | The prefab key as registered in this provider. |
| prefab | The corresponding prefab if found. |
true if a prefab was found; otherwise, false.
|
private |
|
private |
|
private |
|
private |