> For the complete documentation index, see [llms.txt](https://megacrush.gitbook.io/megacrush-unity-assets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://megacrush.gitbook.io/megacrush-unity-assets/runtime-navmesh-baker/runtime-navmesh-baker-user-manual/package-elements/components/navmeshbakerservice/baker-events.md).

# Baker Events

The **BakerEvents** class is a global event hub for runtime baking.

> Note: This is not a separate component you need to add — it is built into the system.

It broadcasts when per-surface, combined-window, or one-shot bakes start, complete, or fail.\
Use these events to trigger gameplay logic after navmesh updates — for example, spawning AI only once a valid navmesh exists.

***

### Available Events

```csharp
// Per-surface events
event Action<DynamicNavMeshSurface, Bounds> OnBakeStarted;
event Action<DynamicNavMeshSurface, Bounds, float> OnBakeCompleted;
event Action<DynamicNavMeshSurface, Bounds, string> OnBakeFailed;

// Combined-window events
event Action<int, Bounds> OnCombinedBakeStarted;
event Action<int, Bounds, float> OnCombinedBakeCompleted;

// One Shot events (new in 1.4.0)
event Action<int, Bounds> OnOneShotBakeStarted;
event Action<int, Bounds, float> OnOneShotBakeCompleted;
```

* **OnBakeStarted(DynamicNavMeshSurface surface, Bounds bounds)**\
  Raised when a per-surface bake begins.\
  `surface` may be `null` in combined or one-shot mode.
* **OnBakeCompleted(DynamicNavMeshSurface surface, Bounds bounds, float seconds)**\
  Raised when a per-surface bake finishes successfully.\
  Includes duration in seconds.
* **OnBakeFailed(DynamicNavMeshSurface surface, Bounds bounds, string reason)**\
  Raised when a bake is skipped or fails.\
  `reason` provides a textual cause (e.g. `"no sources"`).
* **OnCombinedBakeStarted(int agentTypeId, Bounds bounds)**\
  Raised when a combined-window bake begins for a specific agent type.
* **OnCombinedBakeCompleted(int agentTypeId, Bounds bounds, float seconds)**\
  Raised when a combined-window bake completes successfully.
* **OnOneShotBakeStarted(int agentTypeId, Bounds bounds)** *(new in 1.4.0)*\
  Raised when a one-shot bake begins for a given agent type.
* **OnOneShotBakeCompleted(int agentTypeId, Bounds bounds, float seconds)** *(new in 1.4.0)*\
  Raised when a one-shot bake finishes successfully.

***

### Example: Spawn After Bake

```csharp
using MegaCrush.RuntimeNavmeshBaker;
using UnityEngine;

public sealed class SpawnAfterBake : MonoBehaviour
{
    [SerializeField] private GameObject prefab;

    private void OnEnable()
    {
        BakerEvents.OnOneShotBakeCompleted += HandleBakeCompleted;   // new in 1.4.0
        BakerEvents.OnBakeCompleted += HandleBakeCompleted;          // fallback for per-surface mode
    }

    private void OnDisable()
    {
        BakerEvents.OnOneShotBakeCompleted -= HandleBakeCompleted;
        BakerEvents.OnBakeCompleted -= HandleBakeCompleted;
    }

    private void HandleBakeCompleted(DynamicNavMeshSurface s, Bounds b, float dur)
    {
        if (!prefab) return;

        if (UnityEngine.AI.NavMesh.SamplePosition(transform.position, out var hit, 5f, UnityEngine.AI.NavMesh.AllAreas))
        {
            Instantiate(prefab, hit.position, Quaternion.identity);
            Debug.Log($"Spawned {prefab.name} after bake at {hit.position}");
        }
    }
}
```

***

### Best Practices

* **Subscribe/Unsubscribe** in `OnEnable` / `OnDisable` to avoid leaks.
* Use **OnBakeCompleted** or **OnOneShotBakeCompleted** to safely trigger AI spawning.
* Use **OnBakeFailed** for debug logs or fallback behavior.
* Use **Combined events** only if relying on global combined-window mode (Grid Cells + Combine Tiles).
* **Runtime Spawner integration**:
  * Pair with placement policies (*Require*, *Prefer*, *Ignore*).
  * For “Require”, defer spawning until `OnBakeCompleted` or `OnOneShotBakeCompleted` fires.
