Create a custom quad mesh from script
Hi, I'm trying to develop a maze game in which the user has to navigate a ball towards the edge of the platform. The problem is that the way I'm doing it right now creates a number of game objects which massively slow everything down and I was wondering if there was some way I could either combine all of these game objects into one mesh or create a single game object which spans all of the area I need covered in a script.
I've checked out the code at http://wiki.unity3d.com/index.php/ProceduralPrimitives and I'm trying to do something similar to the cube example in which my vertices would be defined by randomly selected locations in my grid (like so):
void createMesh(){
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mesh.Clear();
mf.mesh = mesh;
//vertices
Vector3[] verts = vertices.ToArray();
Vector3 up = Vector3.up;
Vector3 down = Vector3.down;
Vector3 front = Vector3.forward;
Vector3 back = Vector3.back;
Vector3 left = Vector3.left;
Vector3 right = Vector3.right;
Vector3[] normales = new Vector3[]
{
// Bottom
down, down, down, down,
// Left
left, left, left, left,
// Front
front, front, front, front,
// Back
back, back, back, back,
// Right
right, right, right, right,
// Top
up, up, up, up
};
//triangles
int[] tri = triangles.ToArray();
//uvs
Vector2[] uv = uvs.ToArray();
mesh.vertices = verts;
mesh.triangles = tri;
mesh.uv = uv;
mesh.RecalculateBounds();
mesh.Optimize();
}
void Start () {
int n = (int)Random.Range(width*height*0.5f,width*height*0.8f);
for(int i = 0;i < n;i++){
x = Random.Range(0,temp.Count);
foreach(Vector3 vec in temp[x].GetComponent<MeshFilter>().mesh.vertices){
vertices.Add(vec);
vertices.Add(vec + new Vector3(0,1,0));
}
temp.RemoveAt(x);
}
}
As can be seen, all of my needed vectors (SHOULD) be in place for the algorithm and the normals are all there, my problem is with creating the triangles and uvs for the algorithm - I'm kind of stuck at this point, I don't really know how to define all of the triangles or uvs for the vertices I set (or even if I set the vertices right) so I was wondering if there was some tutorial somewhere on creating complex meshes programatically in Unity or if there was some easier way to do this.
Sincere thanks in advance for your help guys :)
Not a tutorial, but an example that maybe is similar to what you want to do. Someone asked how to generating a sprite tilemap from an oldschool ascii tilemap. It procedurally generates a mesh with uv, triangles, color etc. Question. Repo. $$anonymous$$eshBuilder.cs.