Baker Events

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

Note: this is not a separate component that needs to be added, it is built into the system.

It broadcasts when per-tile or combined-window 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

  • OnBakeStarted(DynamicNavMeshSurface surface, Bounds bounds) Raised when a per-tile bake begins. surface may be null in combined mode.

  • OnBakeCompleted(DynamicNavMeshSurface surface, Bounds bounds, float seconds) Raised when a per-tile bake finishes successfully. Includes the 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. Includes the duration in seconds.


Example: Spawn After Bake

using MegaCrush.RuntimeNavmeshBaker;
using UnityEngine;

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

    private void OnEnable()
    {
        BakerEvents.OnBakeCompleted += HandleBakeCompleted;
    }

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

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

        Vector3 pos = transform.position;
        if (UnityEngine.AI.NavMesh.SamplePosition(pos, 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 for gameplay triggers (agents, spawns, navigation)

  • Use OnBakeFailed for debug logs or fallback behavior

  • Use Combined events only if you’re relying on global-combined window mode

  • Prefer global events for simplicity; advanced users can still inspect the NavMeshBakerService for per-scene diagnostics

Last updated