Search code examples
unity-game-enginenavmesh

Unity Navmesh bake multiple maps in the same scene


I have an scene with multiple maps, and when the game starts I randomly activate one of the maps and keep the others inactive.

My problem is that I can only have one the maps baked, because when I change to bake other map it's overwrited by the previous bake.

I search for other post to try baking at runtime but is seems it's not possible.

Is there a way to have multiple bakes or to bake only the active map at runtime?


Solution

  • To solve the problem, you can call the bake navigation code after the level changes.


    Bake Navigation Inside Unity Editor

    This code works similar to what is in the Unity Editor. Just make sure your Navigation Static object is enabled before using it.

    enter image description here

    The NavMeshBuilder class will allow this. In the code bellow.

    using UnityEditor.AI;
    
    ...
    
    public void Generate()
    {
        GenerateLevel(); // for e.g
    
        NavMeshBuilder.BuildNavMesh();
    }
    

    Bake Navigation in Runtime

    To bake in runtime, you need to download the necessary NavMeshComponents.

    The component's will give you a NavMeshSurface component. It does not require static navmesh and works locally. Add component to all of your game ground's then put them in a list as the code bellow. After each run of the game, it is enough to BuildNavMesh all or part of them.

    enter image description here

    public List<NavMeshSurface> surfaces;
    
    public void Start()
    {
        GenerateLevel(); // for e.g
        
        surfaces.ForEach(s => s.BuildNavMesh());
    }
    

    Also this tutorial from Brackeys will help you so much.