Improve FPS with lots of cubes

I have three sets of 50x50 layers of cubes. Unity is amazing for being able to render 7500 cubes. But, I want to know if I can improve performance at all. It’s ok when I look away from the cubes, but on the bottom layer, when I’m looking up, I get about 15 FPS. Can anyone help?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GameController : MonoBehaviour {

    public Vector2 groundSize;
    public GameObject cube;
    public Text text;

    [Range(0, 99)]
    public int scale;

	void Start () {
        generateGround(0);
        generateGround(-5);
        generateGround(-10);
	}

    void generateGround(int baseHeight)
    {
        for (int x = 0; x < groundSize.x; x ++)
        {
            for (int y = 0; y < groundSize.y; y ++)
            {
                GameObject newCube = Instantiate(cube, new Vector3(-groundSize.x / 2 + 0.5f + x, baseHeight, -groundSize.y / 2 + 0.5f + y), Quaternion.Euler(Vector3.zero)) as GameObject;
                newCube.transform.localScale = Vector3.one * scale / 100;
                newCube.transform.name = "newCube";
                newCube.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value);
            }
        }
    }

Thanks.

Well your main problem is probably overdraw.

I can suggest two things:

  1. I am not 100% sure this will work with dynamically created objects, but, in Unity go to Window → Occlusion culling → hit tab Bake, set Smallest occluder to the dimensions of your smallest cube object, and hit Bake button. This should be done relatively fast and will result in your work appearing partitioned into blue sections.

(If it doesn’t work at first, setup one Occluder area as View area and stretch it to the maximum of your playable world)

  1. What material are you using to render cubes? “Standard” material is a PBR - based shader and is very complex. If you’re aiming at Minecraft graphics, try using something from Legacy shader list, such as Diffuse or Bumped Diffuse - they are significantly faster especially in Forward rendering.

This should help to improve performance quite a bit. On a related note, what computer system are you using for development? If you’re getting 15FPS on a relatively powerful CPU+GPU combo, then no shader or overdraw hack will help much and is the matter of your game world algorithm optimization.