I am rebuilding a mesh but something seems odd that I have to create a temp array just to hold the verticies rather than copy an array.
I'm not using list because this is used frequently and I do not want to resize list and instead just use height index boundary to prevent additional garbage collection from resizing lists.
Doesn't work and results in no mesh showing:
Mesh mesh = new Mesh();
//copy myVerticiesArray to mesh.verticies. myVerticiesArray
//length has null values above verticiesIndexHeight but array copy height set in Array.Copy()
Array.Copy(myVerticiesArray, mesh.vertices, verticiesIndexHeight);
[...] code to set UVs etc omitted
mesh.Optimize();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
this.GetComponent<MeshFilter>().mesh = mesh;
Also doesn't work and results in no mesh showing:
Mesh mesh = new Mesh();
//loop through built verticies array to mesh.verticies of correct size because myVerticiesArray
//length has null values above verticiesIndexHeight
mesh.verticies = new Vector3[verticiesIndexHeight];
for(int i = 0; i < verticiesIndexHeight; i++)
{
mesh.verticies[i] = myVerticiesArray[i];
}
[...] code to set UVs etc omitted
mesh.Optimize();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
this.GetComponent<MeshFilter>().mesh = mesh;
Works and renders mesh:
Mesh mesh = new Mesh();
Vector3[] tempVerticiesArray = new Vector3[verticiesIndexHeight];
//copy built verticies array to temp array of correct size because myVerticiesArray
//length has null values above verticiesIndexHeight
Array.Copy(myVerticiesArray, tempVerticiesArray, verticiesIndexHeight);
mesh.verticies= tempVerticiesArray;
[...] code to set UVs etc omitted
mesh.Optimize();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
this.GetComponent<MeshFilter>().mesh = mesh;
mesh.verticies
that should bemesh.vertices
\$\endgroup\$Array.Clone
instead. \$\endgroup\$