1

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?

adrian
  • 97
  • 2
  • 12

1 Answers1

1

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.

KiynL
  • 4,097
  • 2
  • 16
  • 34
  • Thanks for answering but BuildNavMesh() gives a compilation error and BuildNavMeshData() demands me many parameters I do not know. I imported the AI library by using UnityEngine.AI; – adrian Jun 05 '22 at 02:37
  • @adrian I upgraded the answer. The first method only works in Unity editor, it can be used if you generate levels inside, but you can not use it after build. The second method works. – KiynL Jun 05 '22 at 03:11
  • The upgraded answer worked like a charm, I also had to disable a few layer to make it work. Thank you so much. – adrian Jun 05 '22 at 11:47