how to bake NavMesh in runtime from script. I searched in Google but did not find.
some rendered scene and buttons including bake

- 2,249
- 8
- 30
- 66

- 76
- 1
- 1
- 9
-
An image of your code [is not helpful](http://idownvotedbecau.se/imageofcode). Use [markdown](https://stackoverflow.com/editing-help#code) to create a code block instead. – Alexander Sep 05 '18 at 15:59
-
1You can't ("baking" literally means "compile the results before building"). You will need a 3rd party asset for a runtime nav mesh solution. – Draco18s no longer trusts SE Sep 05 '18 at 16:25
4 Answers
Currently, Unity doesn't have a way to bake but NavMesh at runtime BUT there is an experimental package that Unity has that allows you bake a NavMesh at runtime. It is very stable package.
There were tutorials made by Brackeys in collaboration with Unity.
The demo project is available for download on GitHub. You can use in the scripts in there to bake a runtime NavMesh.
I would highly recommend watching the tutorial first.
Here is also the link for Unity's site and tutorials on runtime navmesh.

- 106
- 2
-
The first video you linked just shows how to bake NavMeshes in the editor. The Unity tutorial looks a bit more helpful. – Joel Croteau May 30 '23 at 04:09
using UnityEditor.AI; //"Editor" not "Engine"
NavMeshBuilder.ClearAllNavMeshes();
NavMeshBuilder.BuildNavMesh();
-
5Your answer seems to lack details and examples. Please check on how to write awesome answers here : https://stackoverflow.com/help/how-to-answer – Rishabh Kumar Feb 21 '21 at 06:30
-
2This depends on the editor, doesn't it? It wouldn't work in a final export. Even if this does work, it wouldn't permanently solve the problem. – Michael Macha Dec 31 '21 at 15:25
- Make sure that you installed all required packages "AI Navigation"
- Then you can build the nav mesh by following the code
private void GenerateNavMesh()
{
// Use this if you want to clear existing
//NavMesh.RemoveAllNavMeshData();
var settings = NavMesh.CreateSettings();
var buildSources = new List<NavMeshBuildSource>();
// create floor as passable area
var floor = new NavMeshBuildSource
{
transform = Matrix4x4.TRS(Vector3.zero, quaternion.identity, Vector3.one),
shape = NavMeshBuildSourceShape.Box,
size = new Vector3(10, 1, 10)
};
buildSources.Add(floor);
// Create obstacle
const int OBSTACLE = 1 << 0;
var obstacle = new NavMeshBuildSource
{
transform = Matrix4x4.TRS(new Vector3(3,0,3), quaternion.identity, Vector3.one),
shape = NavMeshBuildSourceShape.Box,
size = new Vector3(1, 1, 1),
area = OBSTACLE
};
buildSources.Add(obstacle);
// build navmesh
NavMeshData built = NavMeshBuilder.BuildNavMeshData(
settings, buildSources, new Bounds(Vector3.zero, new Vector3(10,10,10)),
new Vector3(0,0,0), quaternion.identity);
NavMesh.AddNavMeshData(built);
}

- 71
- 1
- 3
AlienCode's answer is perfect for baking NavMesh at runtime,
since you can't have both using
tags (UnityEditor.AI
& UnityEngine.AI
).
I created a separate script and used it for baking NavMesh.
I'm not certain if it only works in the editor or in builds though.

- 2,249
- 8
- 30
- 66
-
1(*answer above/below* is worse than useless, as the order of answers with same number of votes *do* get ordered variably.) – greybeard Jul 19 '21 at 06:04
-
1You can use both, actually; you just have to declare which import you mean to use. It still isn't a good idea, though, as it renders your project incapable of being distributed to end platforms. UnityEditor is specifically for use with the editor itself, nothing else. – Michael Macha Dec 31 '21 at 15:27