2
\$\begingroup\$

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;
\$\endgroup\$
  • \$\begingroup\$ If I correctly understood Unity docs, when you create a Mesh from scratch Its vertex array and Its uv array, etc, start empty. To copy an array both arrays, source and destination, need to have the same length. If you want to use Array.Copy, first set Mesh.vertices to something that have enough length. If you think in it, at the end you get the same performance hit because of the extra array initialization before using Array.Copy. \$\endgroup\$ – Hatoru Hansou Nov 14 '15 at 21:15
  • \$\begingroup\$ typo in your second and third example? mesh.verticies that should be mesh.vertices \$\endgroup\$ – ratchet freak Nov 14 '15 at 21:56
  • \$\begingroup\$ You could try using Array.Clone instead. \$\endgroup\$ – EvilTak Nov 15 '15 at 5:40
2
\$\begingroup\$

Mesh.vertices is a property, not a variable. According to docs, it:

Returns a copy of the vertex positions or assigns a new vertex positions array.

Which means, when you call mesh.vertices, you don't get the actual vertices, you get a copy of actual vertices. So when you copy something to this array, you actual modify the clone, not the actual data, hence it will have no effect. The third way is the correct way to modify mesh.

There is a reason it is done this way: the actual mesh data may be in GPU memory, so modifying it may be an expensive operation. You should first build you mesh locally, then update the actual data.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.