This is an x-post from the scripting forum (where I was directed here).
I am making a procedurally generated 2D map and have generated a Mesh object containing vertices and triangles that describe the floor locations. I am trying to use this data to build a NavMesh, but I am running into problems. Here is what I currently have (though I have tried quite a few other things):
public class NavmeshGenerator : MonoBehaviour {
NavMeshData navMesh;
NavMeshDataInstance navMeshInstance;
void OnEnable() {
navMesh = new NavMeshData();
navMeshInstance = NavMesh.AddNavMeshData(navMesh);
}
void OnDisable() {
navMeshInstance.Remove();
}
public void Generate(Mesh mesh) {
Destroy(GameObject.Find("Map"));
GameObject map = new GameObject("Map");
var filter = map.AddComponent<MeshFilter>();
filter.mesh = mesh;
Material material = new Material(Shader.Find("Diffuse"));
material.color = Color.red;
var renderer = map.AddComponent<MeshRenderer>();
renderer.material = material;
// ------------------NavMesh Stuff ----------------
NavMeshBuildSource source = new NavMeshBuildSource();
source.transform = map.transform.localToWorldMatrix;
source.shape = NavMeshBuildSourceShape.Mesh;
source.sourceObject = filter.mesh;
source.area = 0;
//source.size = mesh.bounds.size;
var sources = new List<NavMeshBuildSource>();
sources.Add(source);
NavMeshBuilder.UpdateNavMeshData(
navMesh,
NavMesh.GetSettingsByIndex(0),
sources,
mesh.bounds
);
// ---------------End NavMesh Stuff------------------
Destroy(GameObject.Find("Mesh"));
GameObject meshObj = new GameObject("Mesh");
meshObj.transform.position = map.transform.position;
var triangulation = NavMesh.CalculateTriangulation();
var mesh2 = new Mesh();
mesh2.vertices = triangulation.vertices;
mesh2.triangles = triangulation.indices;
meshObj.AddComponent<MeshFilter>();
meshObj.GetComponent<MeshFilter>().mesh = mesh2;
Material material2 = new Material(Shader.Find("Diffuse"));
material2.color = Color.cyan;
meshObj.AddComponent<MeshRenderer>();
meshObj.GetComponent<MeshRenderer>().material = material2;
}
}
The NavMeshGenerator is a component of an empty game object I created in the editor, and I am using 5.6.0f3 which should support this.
The stuff after the NavMesh stuff is just to populate a few variables in a vain attempt at investigating what actually got built. The triangulation always comes back empty, so of course the second renderer has nothing to display. How am I building the NavMesh incorrectly?
Thanks,
-dannuic