- Home /
Mesh UVs won't change positions
So I am trying to change the UVs of a mesh inside the editor and I can't seem to be able to make it work. When I try to assign new UV coordinates to a Mesh variable, the UV coordinates in the variable does not change.
theRect.y += theRect.height / 2;
if (GUI.Button(theRect, "Offset")){
for (i = 0; i < 4; i++){
selectedUVs[i] = Vector2(selectedUVs[i].x + 0.25, selectedUVs[i].y + 0.25);
}
}
//Update the UVs here
for (i = 0; i < 4; i++){
theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i];
}
hitInfo.transform.gameObject.GetComponent(MeshFilter).sharedMesh.uv = theMesh.uv;
The line with theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i];
should be assigning the values properly (to my knowledge anyways). Am I missing something here?
Answer by CHPedersen · Dec 16, 2014 at 10:16 AM
This is because you're setting uvs on the array returned by the .uv property of class Mesh. But this property is not actually a reference directly to the Mesh's own uv array - it is instead a deep copy of all the data it contains. So you're telling Unity to give you a deep copy of the uv array for each iteration of the for-loop here:
theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i];
And then you set the value at an index in each deep copy, and then it gets thrown away again afterwards. So your changes never truly make it into the Mesh's actual uv array.
For the changes to take effect, you need to copy out the uv array ONCE, make changes to the copied array, then ASSIGN IT BACK to the target mesh. Then it'll work. :) Here's an example:
// Grab a reference to the target mesh
Mesh meshYouWantToChange = GetComponent<MeshFilter>().mesh;
// Copy out the contents of its uv array (the property returns a deep copy of the data)
Vector2[] itsUVs = meshYouWantToChange.uv;
// Make changes to that copy
itsUVs[0] = new Vector2(0.5f, 0.5f);
// Assign it back to the target mesh.
meshYouWantToChange.uv = itsUVs;
It works, I'm not entirely sure how it works, but it works! Thank you! :)
I don't understand what a "deep copy" is, so could you explain that please?
And oddly enough I actually did it the way you described in a previous script I made, but thought it would be better if I did it the way I showed in the question.
You're welcome. :)
A 'deep copy' is when a full copy of all the contents of an array is made and returned ins$$anonymous$$d of a copy of the reference to the array.
The difference is that if you receive a copy of the reference, then that will point to the same actual object and thus, modifications to it will affect the original object. That would be a shallow copy. If, like in this case, a property returns a deep copy, you are ins$$anonymous$$d receiving a reference to a whole new, cloned object (the array) that just contains the same data. $$anonymous$$odifications to that copy do not affect the original.
See this Wikipedia article:
http://en.wikipedia.org/wiki/Object_copy
It has a neat little diagram that explains a deep copy. In that diagram, if it was a shallow copy, then there would be only one row of X1, X2, X3,..., and A and B would both be pointing to the same first element.
Okay, that makes more sense now. Thanks for the help! :)
Your answer
Follow this Question
Related Questions
Combine Mesh but Keep UV Offset & Scale 0 Answers
Moving Mesh Instance UVs in Editor? (code provided) 3 Answers
Remove annoying texture offset on custom mesh 0 Answers
Understanding verts and triangles in Unity 1 Answer
Mesh UV min/max Y position 2 Answers