I've written a Method that should create a mesh.
Setting the vertices works, but adding the code to set the triangles crashes the Editor upon execution.
Even though not very efficient (possibly creating duplicate vertices), the could should work in my understanding.
Am I missing something?
public void ShowMovementRange(List reachableNodes)
{
Mesh mesh = _meshFilter.mesh;
Vector3[] vertices = new Vector3[reachableNodes.Count * 4];
int[] triangles = new int[reachableNodes.Count * 6];
float offset = 0.5f;
for (var i = 0; i < reachableNodes.Count; i++)
{
var vertI = i * 4;
var triI = i * 6;
vertices[vertI + 0] = reachableNodes[i].GetPosAsVector3() + new Vector3(-0.5f, offset, -0.5f);
vertices[vertI + 1] = reachableNodes[i].GetPosAsVector3() + new Vector3(-0.5f, offset, 0.5f);
vertices[vertI + 2] = reachableNodes[i].GetPosAsVector3() + new Vector3(0.5f, offset, -0.5f);
vertices[vertI + 3] = reachableNodes[i].GetPosAsVector3() + new Vector3(0.5f, offset, 0.5f);
triangles[triI + 0] = triI + 0;
triangles[triI + 1] = triI + 1;
triangles[triI + 2] = triI + 2;
triangles[triI + 3] = triI + 2;
triangles[triI + 4] = triI + 1;
triangles[triI + 5] = triI + 3;
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
}
↧