I have a simple model that is made from 1 quad. UV1 is mapped to one area of the texture and UV2 is mapped to a different area on the texture. Unity displays the object using UV1. During game play I would like the object to sometimes use UV2 as the texture mapping for this object. How do I tell unity to temporarily use UV2 instead of UV1?
MeshFilter meshFilter = go.GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
Vector2[] temp = mesh.uv;
mesh.uv = mesh.uv2;
mesh.uv2 = temp;
Note that this will cause a copy of the mesh to be created, and the engine will create a new VBO to put the data into.
Unfortunately this is less trivial than it sounds. It can be done in several ways, for example using a script that sets the mesh.uv to the mesh.uv2 set or using a custom shader that can switch between the meshes. An easier method, assuming you don't have a really weird mapping is instead of using 2 uv sets just use one and use texture positioning to switch to different parts of the texture. To give an example:
using UnityEngine;
using System.Collections;
public class UpdateTexture : MonoBehaviour {
public Material background;
void SetVerticalOffset(float verticalOffset) {
Vector2 offset = new Vector2(0f, verticalOffset);
background.SetTextureOffset("_MainTex", offset);
}
}