How does UV mapping via script work?

I’ve been looking into UV mapping, in order to give different sections of the same mesh different textures, and the UV mapping needs to be done programmatically since the mesh’s textures can change at runtime. Although there are already numerous questions and answers on this forum about the topic, I still seemingly cannot understand them. I don’t understand how UV’s, being an array of Vector2s, could tell how to texture an item. Could someone please explain to me how this works?

UV coordinates specify a specific point on a texture by fractions in the range of 0.0 to 1.0.

[14398-uvs.png*|14398]

So a value of (0.5, 0.5) would refer to the center-most pixel in a texture. In meshes, we are specifying the uvs for the vertices (which are used to define triangles). So all the pixels inside the triangles are filled by interpolating between the values we specify for the vertices.

So to experiment a bit:

  • Get the [CreatePlane script from the UnityWiki][2].
  • Create a vertical plane but leave all other setting at default. This will create a two-triangle, four-vertices plane.
  • Add a material with a texture.
  • Attach this script:

#pragma strict

var mesh : Mesh;
var uvs : Vector2[];

function Start() {
	mesh = GetComponent(MeshFilter).mesh;
	uvs = mesh.uv;
	for (var i = 0; i < uvs.Length; i++)
		Debug.Log(uvs*);*

}

function Update() {

// Upper right quarter
if (Input.GetKeyDown(KeyCode.A)) {

  • uvs[0] = Vector2(0.5, 0.5);*
  • uvs[1] = Vector2(1.0, 0.5);*
  • uvs[2] = Vector2(0.5, 1.0);*
  • mesh.uv = uvs;*
  • }*

// Middle quater
if (Input.GetKeyDown(KeyCode.B)) {

  • uvs[0] = Vector2(0.25, 0.25);*
  • uvs[1] = Vector2(0.75, 0.25);*
  • uvs[2] = Vector2(0.25, 0.75);*
  • uvs[3] = Vector2(0.75, 0.75);*
  • mesh.uv = uvs;*
  • }*

//Left half
if (Input.GetKeyDown(KeyCode.C)) {

  • uvs[0] = Vector2(0.0, 0.0);*
  • uvs[1] = Vector2(0.5, 0.0);*
  • uvs[2] = Vector2(0.0, 1.0);*
  • uvs[3] = Vector2(0.5, 1.0);*
  • mesh.uv = uvs;*
  • }*

//A mess
if (Input.GetKeyDown(KeyCode.D)) {

  • uvs[0] = Vector2(0.25, 0.55);*

  • uvs[1] = Vector2(0.0, 0.1);*

  • uvs[2] = Vector2(0.7, 0.2);*

  • uvs[3] = Vector2(0.4, 0.6);*

  • mesh.uv = uvs;*

  • }*
    }
    Pressing the ‘A’, ‘B’, ‘C’ and ‘D’ key resets the vertices in the mesh.
    This simple example doesn’t have an object with multiple materials and submeshes. While the uv concept is simple, identifying the right vertices and assigning the right values can be a challenge.

[2]: http://wiki.unity3d.com/index.php?title=CreatePlane

Every mesh is composed of 1 or more submeshes,
and every submesh has a material ID. So if you have several materials on a mesh, your have several submeshes, each with their material.

Every submesh is composed of triangles, that you can find in the triangle list of your submesh

Every triangle, that is composed of 3 vertices, and each of those vertices have a UV coordinate that correspond to X and Y coordinates in your texture.

so if you know your submesh, you know what material you are using, and thus the texture.
And if you know what triangle, you can get the ID of the vertices of that triangle and thus their UV.