How to create a custom cuboids at runtime?

I am creating a voxel based game (think Minecraft adventure mode) and the performance of having many box colliders and unneeded vertices/draw calls of separate cubes was really low. My first approach was combining the cubes into single mesh, which I did(for each block type) but I still had separate colliders for each block and I still had performance issues when a bigger area was loaded and a huge spike when dynamically loading world around me due to the overhead of creating colliders.

So I decided to compress blocks into bigger cuboids but I can’t work out how to create custom cuboids at runtime(specified by two Vector3s). My first approach was taking the basic cube and scaling it, but I’m not a huge fan of scaling and I had some texturing issues, so I decided to use the Mesh class and create it from scratch. I looked at the examples but it’s a bit unclear, so I am asking for people with experience using the Mesh class to help me work out how to create a custom cuboid.

Thank you for taking your time!

After looking at some examples, I managed to figure it out, it’s only missing UV coords(I just put in default X-Z planar UV coords as a placeholder). The task seemed more intimidating than it actually was. Here’s the code:

EDIT: Split up the vertices for texture mapping and did the texture mapping work. Also added adding a box collider.

using UnityEngine;

using System.Collections;

public static class CreateArbitaryPrimitive
{

public static void CreateCuboid(Vector3 origin, Vector3 end)
{
    Vector3 diff = end - origin;

    Mesh mesh = new Mesh();
    Vector3[] vertices = new Vector3[24];
    Vector2[] uv = new Vector2[vertices.Length];
    int[] triangles = new int[36];
    vertices[0] = new Vector3(0, 0, 0);//side1
    uv[0] = new Vector2(1 / 6f, 0);
    vertices[1] = new Vector3(diff.x, 0, 0);//side1
    uv[1] = new Vector2(0 / 6f, 0);
    vertices[2] = new Vector3(0, diff.y, 0);//side1
    uv[2] = new Vector2(1 / 6f, 1);
    vertices[4] = new Vector3(diff.x, diff.y, 0);//side1
    uv[4] = new Vector2(0 / 6f, 1);
    vertices[3] = new Vector3(0, 0, diff.z); //side2
    uv[3] = new Vector2(1 / 6f, 0);
    vertices[8] = new Vector3(0, diff.y, 0); //side2
    uv[8] = new Vector2(2 / 6f, 1);
    vertices[5] = new Vector3(0, diff.y, diff.z);//side2
    uv[5] = new Vector2(1 / 6f, 1);
    vertices[9] = new Vector3(0, 0, 0);//side2
    uv[9] = new Vector2(2 / 6f, 0);
    vertices[10] = new Vector3(0, diff.y, diff.z);//side3
    uv[10] = new Vector2(3 / 6f, 1);
    vertices[6] = new Vector3(diff.x, 0, diff.z);//side3
    uv[6] = new Vector2(2 / 6f, 0);
    vertices[7] = new Vector3(diff.x, diff.y, diff.z);//side3
    uv[7] = new Vector2(2 / 6f, 1);
    vertices[11] = new Vector3(0, 0, diff.z);//side3
    uv[11] = new Vector2(3 / 6f, 0);
    vertices[12] = new Vector3(diff.x, diff.y, diff.z);//side4
    uv[12] = new Vector2(4 / 6f, 1);
    vertices[13] = new Vector3(diff.x, 0, diff.z);//side4
    uv[13] = new Vector2(4 / 6f, 0);
    vertices[14] = new Vector3(diff.x, diff.y, 0);//side4
    uv[14] = new Vector2(3 / 6f, 1);
    vertices[15] = new Vector3(diff.x, 0, 0);//side4
    uv[15] = new Vector2(3 / 6f, 0);
    vertices[16] = new Vector3(0, 0, 0);//bottom
    uv[16] = new Vector2(5 / 6f, 0);
    vertices[17] = new Vector3(diff.x, 0, diff.z);//bottom
    uv[17] = new Vector2(4 / 6f, 1);
    vertices[18] = new Vector3(0, 0, diff.z);//bottom
    uv[18] = new Vector2(5 / 6f, 1);
    vertices[19] = new Vector3(diff.x, 0, 0);//bottom
    uv[19] = new Vector2(4 / 6f, 0);
    vertices[20] = new Vector3(diff.x, diff.y, diff.z);//top
    uv[20] = new Vector2(1, 1);
    vertices[21] = new Vector3(0, diff.y, 0); //top
    uv[21] = new Vector2(5 / 6f, 0);
    vertices[22] = new Vector3(0, diff.y, diff.z);//top
    uv[22] = new Vector2(5 / 6f, 1);
    vertices[23] = new Vector3(diff.x, diff.y, 0);//top
    uv[23] = new Vector2(1, 0);//top

    #region Triangles

    //sides
    //side1
    triangles[0] = 2;
    triangles[1] = 1;
    triangles[2] = 0;

    triangles[3] = 1;
    triangles[4] = 2;
    triangles[5] = 4;

    //side2
    triangles[6] = 3;
    triangles[7] = 8;
    triangles[8] = 9;

    triangles[9] = 5;
    triangles[10] = 8;
    triangles[11] = 3;

    //side3
    triangles[12] = 10;
    triangles[13] = 6;
    triangles[14] = 7;

    triangles[15] = 11;
    triangles[16] = 6;
    triangles[17] = 10;

    //side4
    triangles[18] = 12;
    triangles[19] = 13;
    triangles[20] = 14;

    triangles[21] = 14;
    triangles[22] = 13;
    triangles[23] = 15;

    //bottom

    triangles[24] = 16;
    triangles[25] = 17;
    triangles[26] = 18;

    triangles[27] = 19;
    triangles[28] = 17;
    triangles[29] = 16;

    //top

    triangles[30] = 22;
    triangles[31] = 20;
    triangles[32] = 21;

    triangles[33] = 21;
    triangles[34] = 20;
    triangles[35] = 23;
    #endregion

    mesh.vertices = vertices;
    mesh.triangles = triangles;
    mesh.uv = uv;

    mesh.RecalculateNormals();
    mesh.RecalculateBounds();

    GameObject go = new GameObject("Cuboid", typeof(MeshRenderer), typeof(MeshFilter), typeof(BoxCollider));
    go.transform.position = origin;
    go.GetComponent<MeshFilter>().mesh = mesh;
    BoxCollider collider = go.GetComponent<BoxCollider>();
    collider.size = mesh.bounds.size;
    collider.center = mesh.bounds.size / 2;
}

}