Hi ,
i edited my NavMesh Gaph via code and now want to use the modifided NavMeshGraph for Debugging purposes. Is there any way to create a Mesh Object from the NavMesh ?
Hi ,
i edited my NavMesh Gaph via code and now want to use the modifided NavMeshGraph for Debugging purposes. Is there any way to create a Mesh Object from the NavMesh ?
Hi
There’s no exposed function for this. But something like this could work:
public static void ExportToFile (NavmeshBase target) {
//INavmesh graph = (INavmesh)target;
if (target == null) return;
NavmeshTile[] tiles = target.GetTiles();
if (tiles == null) {
if (EditorUtility.DisplayDialog("Scan graph before exporting?", "The graph does not contain any mesh data. Do you want to scan it?", "Ok", "Cancel")) {
AstarPathEditor.MenuScan();
tiles = target.GetTiles();
if (tiles == null) return;
} else {
return;
}
}
string path = EditorUtility.SaveFilePanel("Export .obj", "", "navmesh.obj", "obj");
if (path == "") return;
//Generate .obj
var sb = new System.Text.StringBuilder();
string name = System.IO.Path.GetFileNameWithoutExtension(path);
sb.Append("g ").Append(name).AppendLine();
//Vertices start from 1
int vCount = 1;
//Define single texture coordinate to zero
sb.Append("vt 0 0\n");
for (int t = 0; t < tiles.Length; t++) {
NavmeshTile tile = tiles[t];
if (tile == null) continue;
var vertices = tile.verts;
//Write vertices
for (int i = 0; i < vertices.Length; i++) {
var v = (Vector3)vertices[i];
sb.Append(string.Format("v {0} {1} {2}\n", -v.x, v.y, v.z));
}
//Write triangles
TriangleMeshNode[] nodes = tile.nodes;
for (int i = 0; i < nodes.Length; i++) {
TriangleMeshNode node = nodes[i];
if (node == null) {
Debug.LogError("Node was null or no TriangleMeshNode. Critical error. Graph type " + target.GetType().Name);
return;
}
if (node.GetVertexArrayIndex(0) < 0 || node.GetVertexArrayIndex(0) >= vertices.Length) throw new System.Exception("ERR");
sb.Append(string.Format("f {0}/1 {1}/1 {2}/1\n", (node.GetVertexArrayIndex(0) + vCount), (node.GetVertexArrayIndex(1) + vCount), (node.GetVertexArrayIndex(2) + vCount)));
}
vCount += vertices.Length;
}
string obj = sb.ToString();
using (var sw = new System.IO.StreamWriter(path)) {
sw.Write(obj);
}
}