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;
AstarPath.active.Scan();
}
}
.
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
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.