I'm new to Unity. I've created an algorithm to procedurally generate a circle-shaped tunnel made of primitive cubes GameObjects. The problem is that the game is very laggy and I can understand why: there are thousands of objects to render.
What I'd like to know is if there's a better approach to this. For example, would be better if instead of rendering the single cubes I create a ring custom component and use that instead?
Are there any other solutions?
I'm using Unity 5.3.4f1.
EDIT
Here's what I've done so far. This works the way I want but the game is really laggy because (I assume) there are lots of objects to render.
public class TerrainGenerator : MonoBehaviour {
private static int MAX_RING_NUMBER = 1000;
private static int TUNNEL_SLOPE = 5;
// Use this for initialization
void Start () {
int y = 0;
for (int z = 0; z < MAX_RING_NUMBER; z++) {
createRing(0, y, z, 10);
// Every TUNNEL_SLOPE cubes the tunnel goes down a block.
if (z % TUNNEL_SLOPE == 0)
y--;
}
}
// Update is called once per frame
void Update () {
}
private void createCube(int x, int y, int z){
GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
cube.transform.position = new Vector3 (x, y, z);
cube.GetComponent<Renderer>().material.color = new Color(Random.Range(0,255),Random.Range(0,255),Random.Range(0,255));
}
// Midpoint circle alghoritm.
void createRing( int x0, int y0, int z, int radius){
int x = radius;
int y = 0;
int decisionOver2 = 1 - x; // Decision criterion divided by 2 evaluated at x=r, y=0
while( y <= x )
{
createCube(x + x0, y + y0, z); // Octant 1
createCube(y + x0, x + y0, z); // Octant 2
createCube(-x + x0, y + y0, z); // Octant 4
createCube(-y + x0, x + y0, z); // Octant 3
createCube(-x + x0, -y + y0, z); // Octant 5
createCube(-y + x0, -x + y0, z); // Octant 6
createCube(x + x0, -y + y0, z); // Octant 7
createCube(y + x0, -x + y0, z); // Octant 8
y++;
if (decisionOver2<=0)
{
decisionOver2 += 2 * y + 1; // Change in decision criterion for y -> y+1
}
else
{
x--;
decisionOver2 += 2 * (y - x) + 1; // Change for y -> y+1, x -> x-1
}
}
}
}