# Factory ScriptableObjects

he **Runtime Spawner** uses **Factory ScriptableObjects** to define *how* objects are created, pooled, and synchronized at runtime.

Each asset represents a self-contained configuration for spawning in different contexts (single-player, Fusion, PUN, or custom).

These assets are editor-facing wrappers around the core `IObjectFactory` interface. They do not contain any user-editable / facing functionality.

At runtime, the spawner calls the asset’s `Create()` method to build the active factory instance.

> For the underlying architecture and API details, see the\
> [**Factory System Overview** ](https://megacrush.gitbook.io/megacrush-unity-assets/runtime-spawner/runtime-spawner-user-manual/technical-details/factory-system-overview)**→** page.

***

### Overview

| Asset                            | Runtime Factory              | Use Case                                                              | Menu Path                                                               |
| -------------------------------- | ---------------------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| **SinglePlayerPoolFactoryAsset** | `SinglePlayerPoolFactory`    | Local or offline games using the Megacrush Object Pool.               | `Create → Megacrush → Spawner → Factories → Single Player Pool Factory` |
| **FusionFactoryAsset**           | `FusionObjectFactory`        | Photon **Fusion 2.x** networking (authoritative host or shared mode). | `Create → Megacrush → Spawner → Factories → Photon Fusion Factory`      |
| **PUNFactoryAsset**              | `PUNObjectFactory`           | Photon **PUN 2+** networking (peer-to-peer, master client model).     | `Create → Megacrush → Spawner → Factories → Photon PUN 2+ Factory`      |
| **Custom Factory Assets**        | User-defined runtime factory | Extend to support Addressables, ECS, or proprietary backends.         | (User-created subclass of `ObjectFactoryAsset`)                         |

All Factory assets are **ScriptableObjects** stored in your project;\
they can be shared between scenes, swapped per build target, or overridden in runtime profiles.

***

### Usage

1. **Create a Factory Asset**\
   Use the menu path shown above to create your desired factory type.
2. **Assign it to your Runtime Spawner**\
   In the inspector, locate the **Factory** field and assign the asset.
3. **Play Mode**\
   When the scene starts, the spawner automatically calls `factoryAsset.Create()` to instantiate the appropriate runtime factory.

You can maintain multiple factory assets in your project (for example, one for offline testing and one for multiplayer) and switch them via build configuration or bootstrap code.

***

### Supported Integrations

| Integration       | Pool Provider              | Notes                                                                  |
| ----------------- | -------------------------- | ---------------------------------------------------------------------- |
| **Single-Player** | `PoolAdapter`              | Uses the Megacrush Object Pool for local reuse.                        |
| **Fusion 2.x**    | `FusionPoolObjectProvider` | Bridges to Photon Fusion’s object pooling and `NetworkRunner.Spawn()`. |
| **PUN 2+**        | `PUNPoolPrefabProvider`    | Handles prefab registration and pooling under Photon PUN.              |

Each provider implements the same interface, allowing consistent spawn/despawn behavior across modes.

***

### Custom Factory Assets

You can create your own factory type by subclassing `ObjectFactoryAsset`:

```csharp
[CreateAssetMenu(menuName = "Megacrush/Spawner/Factories/Addressable Factory")]
public sealed class AddressableFactoryAsset : ObjectFactoryAsset
{
    public override IObjectFactory Create()
    {
        return new AddressableFactory();
    }
}
```

This approach lets you integrate any custom pooling or streaming system while keeping full compatibility with the Runtime Spawner API.

***

### Summary

* Factory assets define the **spawn backend** used by the spawner.
* They can be swapped at any time without changing your scene setup.
* All assets share the same base class, `ObjectFactoryAsset`.
* Networking factories automatically configure pooling and synchronization for their respective backends.

> **Next:**\
> Read the [Factory System Overview](https://megacrush.gitbook.io/megacrush-unity-assets/runtime-spawner/runtime-spawner-user-manual/technical-details/factory-system-overview) for diagrams, API details, and custom factory examples.
