How to texture walls in procedurally generated mesh?

I’m trying to make a simple tile engine for my game.

I have some basic experience with 2D but I never programmed in 3D before… I read that creating too many objects is not optimal and since the game’s going to have some big maps, I decided to render the randomly generated world and levels using single meshes rather than creating one object per tile.
I started with quill18’s tutorial on youtube and I’m using the code as a base to develop the engine and learn more about Unity and 3D programming in general.

Now, I’ve rewritten most of that code and I managed to correctly generate the mesh the way I want it… triangles, uvs, normals and lighting seems to be fine as well. There are no shared vertices, every vertex must have its own normal for lighting to work properly, so basically every corner has duplicates.

The only thing I couldn’t figure out is how to deal with wall textures.

Here’s what I have so far:

17437-top.png

17438-side.png

and the code to generate the texture:

void BuildTexture() {
	
	int texWidth = ( size_x ) * tileResolution;
	int texHeight = ( size_z ) * tileResolution;
	Texture2D texture = new Texture2D( texWidth, texHeight );
	
	// turn the tileset texture into an array
	Color[][] tiles = ChopUpTiles();

	for( int z = 0; z < size_z; z++ ) {
	    for( int x = 0; x < size_x; x++ ) {
            Color[] p = tiles[ map.GetType( x, z ) ];
            texture.SetPixels ( x * tileResolution,
                                z * tileResolution,
                                tileResolution,
                                tileResolution,
                                p );
	    }
	}
	
	texture.filterMode = FilterMode.Point;
	texture.Apply();
	
	MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
	mesh_renderer.sharedMaterial.mainTexture = texture;
	
	Debug.Log ("Texture done!");
}

Color[][] ChopUpTiles() {
	int numTilesPerRow = terrainTiles.width / tileResolution;
	int numRows = terrainTiles.height / tileResolution;
	Color[][] tiles = new Color[ numTilesPerRow * numRows ][];
	
	for( int y = 0; y < numRows; y++ ) {
	    for( int x = 0; x < numTilesPerRow; x++ ) {
		tiles[ y * numTilesPerRow + x ] = terrainTiles.GetPixels(
                                              x * tileResolution,
                                              y * tileResolution,
                                              tileResolution,
                                              tileResolution);
		}
	}
	return tiles;
}

where size_x and size_z are the number of tiles in that axis, tileResolution is the resolution of every tile in the tileset (e.g. it’s 16 for 16 x 16 pixels)

To be honest I’m having trouble understanding the walls’ texturing process… I understood what it takes to texture a flat mesh using a tileset, but this is where I got lost. What I’d like to achieve is a way to texture the walls correctly (e.g. maybe with the ‘W’ tile texture repeating vertically).

Every single tutorial / guide / documentation always textures pretty simple objects and never touches something like this, so I’m looking for some pointers.

try to make a cube in blender and use the UV MAP