Texturing generated mesh grid with texture atlas

Hey guys! I’m fairly new to unity and I’m working on a project where I’ve created a mesh plane, subdivided into 100x100 quads, each of which I want to texture procedurally with different tiles (i.e. It’s the game map for a top-down tile-based game. From what I’ve gathered, the best way to approach is to doing this is with a texture atlas. Like I said, I’m new to Unity and 3D dev in general, so if anyone could help me with how to get this working, I would be very very grateful.

Here is how I am creating the mesh

public class CreateMesh : MonoBehaviour {
	public GameObject theObj;
	
	int sectionsWidth = 100;
	int sectionsHeight = 100;
	Vector3 meshNormal = Vector3.up;
	
	
	// Use this for initialization
	void Start () {
		
		Mesh mesh = new Mesh();
		mesh.name = "testMesh";
		mesh.Clear();
				
		Vector3[] vertices = new Vector3[(sectionsWidth*sectionsHeight)];
		Vector2[] meshUVs = new Vector2[(sectionsWidth*sectionsHeight)];
		Vector3[] meshNormals = new Vector3[(sectionsWidth*sectionsHeight)];
		int[] meshTriangles = new int[3*(sectionsWidth*sectionsHeight)*2];
		
		for(int i = 0; i < sectionsWidth; i++) {
			for(int j = 0; j < sectionsHeight; j++) {
				vertices[j*sectionsWidth + i] = new Vector3((float)i, 0f, (float)(j));
				meshUVs[j*sectionsWidth + i] =  new Vector2((i/2.0f)/sectionsWidth,(i/2.0f)/sectionsHeight);
				meshNormals[j*sectionsWidth + i] = meshNormal;
			}
		}
		
		int index = 0;
        for (int y = 0; y < sectionsWidth-1; y++) {
            for (int x = 0; x < sectionsWidth-1; x++) {
                // For each grid cell output two triangles
                meshTriangles[index++] = (y     * sectionsWidth) + x;
                meshTriangles[index++] = ((y+1) * sectionsWidth) + x;
                meshTriangles[index++] = (y     * sectionsWidth) + x + 1;
    
                meshTriangles[index++] = ((y+1) * sectionsWidth) + x;
                meshTriangles[index++] = ((y+1) * sectionsWidth) + x + 1;
                meshTriangles[index++] = (y     * sectionsWidth) + x + 1;
            }
        }
		
		mesh.vertices = vertices;
      		mesh.triangles = meshTriangles;
	 	mesh.normals = meshNormals;
		mesh.uv = meshUVs;
	
		MeshFilter mf = (MeshFilter)theObj.gameObject.GetComponent(typeof(MeshFilter));
		MeshRenderer mr = (MeshRenderer)theObj.gameObject.GetComponent(typeof(MeshRenderer));
		mf.mesh = mesh;
		mr.renderer.material.color = Color.red;
		
		mesh.RecalculateBounds();
	}

your approach looks correct to me.
What is your problem exactly?
You need to create on big texture that would hold your tiles. Say if your texture is 1024x1024 and your tiles are 64x64 then you can fit 32 tiles in this texture.
Then you would change the uv coords of your vertices to pick up the right tile from the texture.
so to get the first tile (top left) you would asiigne uvs: (0,0), (64/1024,0), (64/1024,64/1024), (0,64/1024) in the right order to the 4 vertices of your tile. (assuming you have 4 vertices per tile)

Ah thanks. Is there some way to create a new texture out of materials you have in your project?

Texture2D.PackTextures.

–Eric

Have I stuffed up the quads? If quads share vertices, I won’t be able to set the uvs correctly, correct?

Correct.

–Eric