A* Pathfinding Project

Generate Navmesh Graph from Multiple Procedural Meshes


I can update one navmeshgraph at runtime using this code from another post.

var graph = AstarPath.active.astarData.navmesh;
graph.sourceMesh = mymesh.mesh;

However once I add more than one navmesh graph only one gets generated from the procedural mesh. The other is generated from the original mesh.

How can you access the Source Mesh, Scale, Rotation Ect for each individual Navmesh Graph shown in the inspector?

Is it possible to add a new graph during runtime?



Yeah, the .navmesh property is just a shortcut to the first navmesh graph. To access all of them you can access the graphs field.

foreach (var graph in AstarPath.active.data.graphs) {
     var navmesh = graph as NavMeshGraph;
     if (navmesh != null) // then this is a navmesh graph

You can add graphs using the AstarPath.active.data.AddGraph method.


Thanks Aron this worked like a charm.
I was so close, I missed the cast to a NavMeshGraph.
I can now scan several meshes and adjust their scale, position, and rotation at runtime!

foreach( var graph in AstarPath.active.data.graphs){

        var navmesh = graph as Pathfinding.NavMeshGraph;
        if (navmesh.sourceMesh.name == myMesh.name){

            navmesh.scale = transform.localScale.x; //assumes uniform scale
            navmesh.rotation = transform.rotation.eulerAngles;
            navmesh.offset = transform.position;
            navmesh.sourceMesh = myMesh;



I have found a limitation. This method gives you an array of graphs, which you can access by index or even by the mesh name. However if two navmesh graphs are generated from the same mesh they have the same name, and it becomes impossible to distinguish them.

In my example I have two procedural planets generated from a sphere mesh. I have had to copy and rename one of the spheres to “moon” so that I could match the mesh with the planet. Which means I will not be able to randomly generate more planets.

It would be nice if the graph had a unique id, or a tag.
This would allow me to match the navmesh to the planet, even if it was generated from the same mesh.

Am I missing something?
I plan to try to add this functionality. Any suggestions as to where I should start?Preformatted text


Looks like NavGraph has a guid variable:
“Used as an ID of the graph, considered to be unique.”

Sounds like what you’re after?



Nice that you succeeded.

As @Hunted notes, you could use the GUID field. However I’m not sure why you couldn’t just keep a reference to the graph? All graphs that you use are completely identical except for the settings you set in that loop, aren’t they?

Also. You should move the Scan call to outside the foreach loop. Right now you are re-scanning all graphs every time you set the settings for a graph, which is a bit much. You only have to scan the graphs once after you have set all the settings.