C# Uv mapping problem

Ok so I have been learning to uv map proocedural meshes in unity. The problem I am having is I dont understand how to wrap the whole thing, I understand how to uv a plane but a 3d cube, please help! Code: `using UnityEngine;
using System.Collections;

public class BlockDirt : MonoBehaviour{

Block block;

public void Start(){

	block = new Block();
	block.setBlockName("test");

	blockCreate(this.transform.position);

	Vector3 prevPos = this.transform.position;

	this.gameObject.AddComponent<MeshFilter>().mesh=blockCreate(this.transform.position);
	this.gameObject.AddComponent<MeshRenderer>();
	//this.gameObject.GetComponent<MeshRenderer>().materials[0] = (Material)Resources.Load("Textures/Blocks/BlockMat");
	
	this.transform.position=prevPos;

	Debug.Log("test");
}

public Mesh blockCreate(Vector3 pos){

	Mesh mesh = new Mesh();

	int scale = 2;

	Vector3[] verts = new Vector3[8];
	Vector2[] uvs = new Vector2[12];
	int[] tris;

	verts[0]=pos+new Vector3(0,0,0);
	verts[1]=pos+new Vector3(scale,0,0);
	verts[2]=pos+new Vector3(scale,0,scale);
	verts[3]=pos+new Vector3(0,0,scale);
	verts[4]=pos+new Vector3(0,scale,0);
	verts[5]=pos+new Vector3(scale,scale,0);
	verts[6]=pos+new Vector3(scale,scale,scale);
	verts[7]=pos+new Vector3(0,scale,scale);

	tris=new int[]{0,1,2,
		0,2,3,
		0,5,1,
		0,4,5,
		4,6,5,
		4,7,6,
		3,6,7,
		3,2,6,
		0,7,4,
		0,3,7,
		1,5,6,
		1,6,2

	};

	uvs[0]=new Vector2(0,0);
	uvs[1]=new Vector2(0,0.125f);
	uvs[2]=new Vector2(0.125f,0.125f);
	uvs[3]=new Vector3(0.125f,0);

	uvs[4]=new Vector2(0,0);
	uvs[5]=new Vector2(0,0.125f);
	uvs[6]=new Vector2(0.125f,0.125f);
	uvs[7]=new Vector3(0.125f,0);
	
	mesh.vertices = verts;
	mesh.triangles = tris;

	mesh.RecalculateNormals();

	mesh.uv = uvs;
	//mesh.RecalculateBounds();
	return mesh;

}

}`

If you understand how to UV-map a plane i don’t understand your question i guess. There is no “standard way” of unwrapping any more complex mesh. The Unity default cube mesh simply maps each quad face to the whole texture. That way you see the same texture on all 6 faces. If you want to show different portions of the texture on different faces, you have to map each face accordingly.

Generally there are different ways how to “see” / understand uv coordinates. In my oppinion it’s the easiest to just see them as plain coordinates as well, just like the actual vertex position. It’s just a position in a different space, the UV / Texture space. The UV coordinates from the same face but are used like a stencil to specify which part of the texture will be seen on that face / triangle.

From your code above it looks like you only have 8 vertices. That most likely won’t work out. Another thing most people don’t realise is that a single vertex isn’t just the vertex position but all vertex attributes together. Those are: vertex position, color, normal, uv, tangent… If a one face need a different value in any of those attributes, you have to copy the whole vertex in order to change that one attribute only for that one face.

That’s why the default cube in Unity has 24 instead of 8 vertices. For a lit cube you need at least 24 vertices since each face need it’s own vertex normals or the cube will be rendered as the most lowpoly sphere. Take a look at this great vid to get a glimpse.

What you need to do is to duplicate each vertex two times since each corner of the cube is used by 3 different faces but each face needs a different vertex normal and probably a different UV coordinates.

How you lay out the 6 different faces on the texture is up to you. A cube most the time is unwrapped as a t-shape (either a small “t” or a capital “T”) but it’s not a requirement at all. Some unwrapse might look like this or in the case of minecraft forexample each face has completely different uvs to map a certain area from the texture-atlas.

It almost looks like you try to do something like minecraft. If you’re trying to do something like minecraft, it won’t work that way. You can’t render each cube on it’s own. Just a 100x100x100 section would contain 1 million cubes. You have to create chunks where 1 mesh contains many cubes at once. In mc they used 16x256x16 chunks. That won’t work in Unity since Unity has a vertex limit of 65000 but you would need 1.5 million, so you need to use smaller chunks.

There is already the MinePackage(minecraft starter package) on the forums which does most things for your. Even when you’re not going to use it, it might help you to understand some basic concepts.