Creating walls in code

So I have a top down game with 3D walls that jut all the way out. What I want to do is to make the textures repeat to fit whatever scale the wall is. How would I go about doing this?
Here is what they are currently looking like:

4 years later still no reply

It’s a good thing you bumped it though. Perhaps Afropenguinn can now finally get the answer he has been eagerly awaiting.

Actually, I abandoned this project shortly after this, and restarted it just a couple months ago! I have learned a lot since then, here is the code I use as well as an explanation of how it works:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wall : MonoBehaviour
{
    public Vector2 size = Vector2.one;
    private const float depth = 10f;

    public Material material;
    public WallMaterials individualSides;
    private Material leftMaterial;
    private Material rightMaterial;
    private Material topMaterial;
    private Material bottomMaterial;

    [System.Serializable]
    public struct WallMaterials
    {
        public Material leftMaterial;
        public Material rightMaterial;
        public Material topMaterial;
        public Material bottomMaterial;
    }

    private Mesh mesh;

    private Vector2 bottomLeft;
    private Vector2 topRight;

    private void Awake()
    {
        //cache size data
        bottomLeft = new Vector2(size.x / -2f, size.y / -2f);
        topRight = new Vector2(size.x / 2f, size.y / 2f);

        //get what materials to use
        if (individualSides.leftMaterial != null)
            leftMaterial = individualSides.leftMaterial;
        else
            leftMaterial = material;

        if (individualSides.rightMaterial != null)
            rightMaterial = individualSides.rightMaterial;
        else
            rightMaterial = material;

        if (individualSides.topMaterial != null)
            topMaterial = individualSides.topMaterial;
        else
            topMaterial = material;

        if (individualSides.bottomMaterial != null)
            bottomMaterial = individualSides.bottomMaterial;
        else
            bottomMaterial = material;

        //generate a wall mesh
        mesh = GenerateMesh();

        //create a mesh filter and give it the mesh
        gameObject.AddComponent<MeshFilter>().mesh = mesh;
        gameObject.AddComponent<MeshRenderer>().materials = new Material[] { leftMaterial, rightMaterial, topMaterial, bottomMaterial };

        //add the box collider
        gameObject.AddComponent<BoxCollider2D>().size = new Vector2(size.x, size.y);

        //destroy ourselves, we are no longer needed
        DestroyImmediate(this);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.black;
        Matrix4x4 rotationMatrix = Matrix4x4.TRS(transform.position, transform.localRotation, transform.localScale);
        Gizmos.matrix = rotationMatrix;
        Gizmos.DrawCube(Vector3.zero, new Vector3(size.x, size.y, 0f));
    }

    private Mesh GenerateMesh(string name = "Generated Mesh")
    {
        //setup mesh
        Mesh mesh = new Mesh();
        mesh.name = name;
        mesh.subMeshCount = 4;

        //get verticies
        Vector3[] vertices = new Vector3[16];
        vertices[0] = new Vector3(bottomLeft.x, topRight.y, 0f); //left wall
        vertices[1] = new Vector3(bottomLeft.x, bottomLeft.y, 0f);
        vertices[2] = new Vector3(bottomLeft.x, topRight.y, -depth);
        vertices[3] = new Vector3(bottomLeft.x, bottomLeft.y, -depth);
        vertices[4] = new Vector3(topRight.x, topRight.y, 0f); //right wall
        vertices[5] = new Vector3(topRight.x, bottomLeft.y, 0f);
        vertices[6] = new Vector3(topRight.x, topRight.y, -depth);
        vertices[7] = new Vector3(topRight.x, bottomLeft.y, -depth);
        vertices[8] = new Vector3(bottomLeft.x, topRight.y, 0f); //top wall
        vertices[9] = new Vector3(topRight.x, topRight.y, 0f);
        vertices[10] = new Vector3(bottomLeft.x, topRight.y, -depth);
        vertices[11] = new Vector3(topRight.x, topRight.y, -depth);
        vertices[12] = new Vector3(bottomLeft.x, bottomLeft.y, 0f); //bottom wall
        vertices[13] = new Vector3(topRight.x, bottomLeft.y, 0f);
        vertices[14] = new Vector3(bottomLeft.x, bottomLeft.y, -depth);
        vertices[15] = new Vector3(topRight.x, bottomLeft.y, -depth);
        mesh.vertices = vertices;


        //get triangles
        int[] triangles = new int[6];
        triangles[0] = 2; //left wall
        triangles[1] = 1;
        triangles[2] = 0;
        triangles[3] = 2;
        triangles[4] = 3;
        triangles[5] = 1;
        mesh.SetTriangles(triangles, 0);

        triangles[0] = 4; //right wall
        triangles[1] = 5;
        triangles[2] = 6;
        triangles[3] = 5;
        triangles[4] = 7;
        triangles[5] = 6;
        mesh.SetTriangles(triangles, 1);

        triangles[0] = 8; //top wall
        triangles[1] = 9;
        triangles[2] = 10;
        triangles[3] = 9;
        triangles[4] = 11;
        triangles[5] = 10;
        mesh.SetTriangles(triangles, 2);

        triangles[0] = 14; //bottom wall
        triangles[1] = 13;
        triangles[2] = 12;
        triangles[3] = 14;
        triangles[4] = 15;
        triangles[5] = 13;
        mesh.SetTriangles(triangles, 3);

        //get uvs
        Vector2[] uvs = new Vector2[vertices.Length];
        float yTiling = size.y / (leftMaterial.mainTexture.width / 100f); //left wall
        float zTiling = depth / (leftMaterial.mainTexture.height / 100f);
        float yOffset = -(yTiling - (int)yTiling) / 2f;
        float zOffset = -(zTiling - (int)zTiling) / 2f;
        uvs[0] = new Vector2(yOffset, zOffset);
        uvs[1] = new Vector2(yTiling + yOffset, zOffset);
        uvs[2] = new Vector2(yOffset, zTiling + zOffset);
        uvs[3] = new Vector2(yTiling + yOffset, zTiling + zOffset);

        yTiling = size.y / (rightMaterial.mainTexture.width / 100f); //right wall
        zTiling = depth / (rightMaterial.mainTexture.height / 100f);
        yOffset = -(yTiling - (int)yTiling) / 2f;
        zOffset = -(zTiling - (int)zTiling) / 2f;
        uvs[4] = new Vector2(yTiling + yOffset, zOffset);
        uvs[5] = new Vector2(yOffset, zOffset);
        uvs[6] = new Vector2(yTiling + yOffset, zTiling + zOffset);
        uvs[7] = new Vector2(yOffset, zTiling + zOffset);

        float xTiling = size.x / (topMaterial.mainTexture.width / 100f); //top wall
        zTiling = depth / (topMaterial.mainTexture.height / 100f);
        float xOffset = -(xTiling - (int)xTiling) / 2f;
        zOffset = -(zTiling - (int)zTiling) / 2f;
        uvs[8] = new Vector2(xTiling + xOffset, zOffset);
        uvs[9] = new Vector2(xOffset, zOffset);
        uvs[10] = new Vector2(xTiling + xOffset, zTiling + zOffset);
        uvs[11] = new Vector2(xOffset, zTiling + zOffset);

        xTiling = size.x / (bottomMaterial.mainTexture.width / 100f); //bottom wall
        zTiling = depth / (bottomMaterial.mainTexture.height / 100f);
        xOffset = -(xTiling - (int)xTiling) / 2f;
        zOffset = -(zTiling - (int)zTiling) / 2f;
        uvs[12] = new Vector2(xOffset, 0f);
        uvs[13] = new Vector2(xTiling + xOffset, 0f);
        uvs[14] = new Vector2(xOffset, zTiling);
        uvs[15] = new Vector2(xTiling + xOffset, zTiling);

        mesh.uv = uvs;

        //return mesh
        return mesh;
    }
}

So what this does is it takes in a size (x and y since this is for 2D) and either a single material for the whole wall or a material per side. From there it creates the mesh and sets up the appropriate UVs. Because the mesh is generated you avoid using the transform to scale your wall objects, instead having a proper variable for the size. The UV mapping is set up to repeat the texture across the wall, based on the size of the texture. If the texture doesn’t perfectly fit the wall, it will center the texture. I do use a magic number though, 100, which is the default pixel per unit ratio in unity. You could change that to be dynamic if you want, I just tend not to change the defaults on that. Lastly, this could easily be ran in the editor if you like, to avoid having to generate at runtime. Mine doesn’t do this since it was built for procedurally generated levels.

Each side of the wall is a submesh, allowing each side to be textured differently. This isn’t the most efficient thing in the world, as you could have a single mesh with one material that fits across the entire wall, but ultimately the point of this was for ease of use and procedural generation. If you only ever wanted a single texture for all four sides, I’d recommend making it a single mesh instead. As a side note, the class for holding individual sides was just because I was too lazy to make a custom editor at the time, it’s purely to make it look pretty in the inspector.

So long story short the answer is: Generate a mesh with UV mapping to fit your material. If you have any questions, feel free to ask! Though before you do, I’d recommend reading up on how meshes are stored and UV mapping, as these are pretty broad topics that I’d rather not get into here. There is plenty of info on this from the fine folks of this very forum!