Quantcast
Channel: Question and Answer » uv-mapping
Viewing all articles
Browse latest Browse all 11

Unity, Cannot change a single Vector2 element of mesh.uv?

$
0
0

I have a simple procedurally generated mesh, with uvs that tile correctly

for(int i=1; i<vertices; i++) {
    uv[i] =  new Vector2(x,z);  //pseudocode
}
mesh.uv = uv;

This works fine. However if I later attempt to change one of the mesh.uv entries

for(<loop over four nodes on tile>) {
    Debug.Log("before: " + mesh.uv[vertex_index].ToString()); // print before

    mesh.uv[vertex_index] =  new Vector2(new_x,new_y);

    Debug.Log("after: " + mesh.uv[vertex_index].ToString()); // print after
}

I get no change in mesh.uv.

What am I missing about unity mesh storage? This works fine for a simple array of Vector2:

Vector2[] v2 = {Vector2.one, Vector2.one};
for (int j = 0; j < 2; j++) {
    Debug.Log(v2[j]);
    v2[j] = new Vector2(2,3);
    Debug.Log(v2[j]);
    }

EDIT:
I can make a successful uv-change by making a copy of the mesh.uv, changing the Vector2 values in the copy and then assigning mesh.uv = copy.

Vector2[] uvCopy = mesh.uv;
for(changed vertices) {
    uvCopy[vertex_index] = new Vector2(new_x, new_y);
}
mesh.uv = uvCopy;

I still would like to know what was wrong with my original approach, if anyone has any ideas.


Viewing all articles
Browse latest Browse all 11

Trending Articles