I am making a tile based game. I have many different 3d tiles using simple textures, which have been merged in Photoshop into the texture below.
Each tile has an id number which I use to instantiate the correct 3d tile at runtime.
My question is how should I offset the texture to ensure each tile uses the correct part of the texture UV.
The texture is made up of a 10x10 grid. The current code applies the texture for the bottom left corner to the whole selection.
MeshRenderer tileRender = _tile.transform.GetComponentInChildren<MeshRenderer>();
MeshFilter tileFilter = _tile.transform.GetComponentInChildren<MeshFilter>();
tileRender.material = crunchTex;
//Build the uvs
Vector2 uvs = new Vector2[tileFilter.sharedMesh.vertices.Length];
uvs = tileFilter.sharedMesh.uv; // Take the existing UVs
//get correct positions in array (it is a 10x10 grid
float tempxOffset = (_id % 10);
xOffset = (tempxOffset /10);
xOffset = (1 - xOffset);
yOffset = (_id / 10);
yOffset = Mathf.Ceil(yOffset);
yOffset = (yOffset/10);
for (var i = 0 ; i < uvs.Length; i++)
uvs[i] = new Vector2(uvs[i].x, uvs[i].y); // ???
// Apply the modded UV's
tileFilter.sharedMesh.uv = uvs;
May 17 '12 at 07:11 AM