I have created a large cubic lattice of thousands of cubes (or spheres), and I want to procedurally control their colour (so I can, for example, make it appear that a wave of colour moves through the lattice via controlling the cubes’ colours). Here is the code:
void Start () {
//initialize cube field
cubes = new GameObject[cubeFieldSize [0], cubeFieldSize [1], cubeFieldSize [2]];
for (int i = 0; i < cubeFieldSize[0]; i++) {
for (int j = 0; j < cubeFieldSize[1]; j++) {
for (int k = 0; k < cubeFieldSize[2]; k++) {
//instantiate array of objects (cubes or spheres)
if(preferSpheres){
cubes[i,j,k] = (GameObject)Instantiate (sphere, transform1.position, Quaternion.identity);
}
else{
cubes[i,j,k] = (GameObject)Instantiate (cube, transform1.position, Quaternion.identity);
}
//make children of parent
cubes[i,j,k].transform.parent = transform;
//set up positions of objects
cubes[i,j,k].transform.position = new Vector3 ((i-cubeFieldSize[0]/2+0.5f)*cubeSpacing,
(j-cubeFieldSize[1]/2+0.5f)*cubeSpacing,
(k-cubeFieldSize[2]/2+0.5f)*cubeSpacing);
//set up scale of objects
cubes[i,j,k].transform.localScale = new Vector3 (cubeSize,cubeSize,cubeSize);
}
}
}
}
I am controlling the colour of the objects using
cubes[i,j,k].GetComponent<Renderer>().material.color = colours[m];
My problem is that I would like to render a lot of objects, but of course the larger the number the more it slows. Is there a more efficient way to render 100,000+ objects?
I thought maybe I could use a particle system, since I don’t really need to draw entire meshes for my purpose, just circles or lights would do?
Or I saw one Unity Answer that talked about procedural meshes?
Any suggestion of how these might be implemented in my case, with the colours being controlled, and so I can draw many more objects than I can at the moment, would be much appreciated.