x


Switch between uv and uv2 on one texture

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?

more ▼

asked Dec 09 '09 at 06:29 AM

ibyte gravatar image

ibyte
544 12 18 27

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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.

more ▼

answered Dec 09 '09 at 02:18 PM

Lance Sun gravatar image

Lance Sun
1k 12 16 41

(comments are locked)
10|3000 characters needed characters left

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);
    }
}
more ▼

answered Dec 09 '09 at 07:20 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

While I accepted the answer below I think I will end up using your approach. Thanks

Dec 09 '09 at 03:30 PM ibyte
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2206
x818
x238

asked: Dec 09 '09 at 06:29 AM

Seen: 3235 times

Last Updated: Dec 09 '09 at 06:29 AM