Common API Examples

Runtime Navmesh Baker – Common API Examples

This page shows the most common tasks you’ll perform in game code when working with the BakerCoordinator.


Getting a Coordinator

// Recommended: reference the one you’ve placed in your scene
var coord = BakerCoordinator.Active;

// Fallback: search the scene (slower, but safe if only one exists)
var coord = FindObjectOfType<BakerCoordinator>();

If you expect there might not be one in the scene, you can call BakerCoordinator.Ensure() to auto-create one.


Setting the Target to Follow

Tell the baker which object (usually your player or camera) should act as the bake center:

coord.SetCenterTarget(player.transform);
  • By default this immediately triggers a bake.

  • You can disable the immediate rebake:

coord.SetCenterTarget(player.transform, rebakeNow: false);

Starting Baking Manually

If autoStart is off (or you want to trigger manually):

coord.StartBaking();

This will:

  • Bake around the current target (Continuous / Combined modes).

  • Bake the active grid cells (Grid mode).

Aliases:

coord.BakeNow();     // same as StartBaking()
coord.BakeAllNow();  // force all registered surfaces at once

Marking Surfaces Dirty

If you modify geometry at runtime:

coord.MarkSurfaceDirty(surface);

Optional overloads let you pass bounds, agent type, or priority.


Checking Bake Status

Debug.Log($"Queued: {coord.QueuedCount}, In-Flight: {coord.InFlightCount}");

if (coord.TryGetLastCombinedBounds(out var b))
    Debug.Log($"Last baked bounds: {b}");

Example: Spawning a Player Dynamically

void SpawnPlayer()
{
    var player = Instantiate(playerPrefab);
    var agent = player.GetComponent<UnityEngine.AI.NavMeshAgent>();
    agent.enabled = false; // disable until navmesh is ready

    var coord = BakerCoordinator.Active;
    coord.SetCenterTarget(player.transform);

    // Enable the agent after the first bake completes
    NavMeshBakerService.OnBakeCompleted += OnBakeCompletedOnce;
    void OnBakeCompletedOnce(NavMeshBakerService.BakeResult r)
    {
        agent.enabled = true;
        NavMeshBakerService.OnBakeCompleted -= OnBakeCompletedOnce;
    }
}

Quick Reference

  • SetCenterTarget(Transform, bool) → set follow target.

  • StartBaking() / BakeNow() → enqueue an initial bake.

  • BakeAllNow() → bake all registered surfaces.

  • RegisterSurface() / UnregisterSurface() → manage surfaces.

  • MarkSurfaceDirty() → force rebake of a surface region.

  • QueuedCount / InFlightCount → runtime stats.

Last updated