What is the most efficient way to spawn in a lot of cubes, like 10k? [c#]

Ok, so i’ve been testing to spawn in a lot of cubes, in a grid way(50x50 or 100x100) but the issue is that
it freezes the Unity Editor for a long time, when i spawn in a huge amount of cubes. At the moment im using the primitive cubes that Unity comes with, and i spawn them in one time, all of them at the start.

Im wondering is there any better ways to instantiate cubes in a huge amount?
Like should i instantiate small chunks until it reaches the goal of amount of tiles i want?

So if im able to fix the first issue, and im able to make a huge grid of cubes in faster way, then im think im gonna have two new issues.

First one is that if i were able to do 200x200 then it would mean 40000 cubes/object to check for in a loop.
How would i be able to loop through so many objects, without big performance issues?

Then the other one is the graphical part, where 200x200 cubes would may lag the game, fps wise.

Update: Below is a 75x75 grid of cubes, every tile here is one cube, and all the cubes will be viewed from above, so i’ve would want to achieve something like this, but cheaper, faster and pleasant for the computer

It depends on what you’re using them for. I’ve tried doing this for a minecraft type game, and it does not work.

I used a loop to create the level and it didn’t work very well. I have a game in development that uses lots of cubes as a core game mechanic, and I use Occlusion to hide the cubes not visible to the camera.

Spawning lots of cubes with Rigidbodies will fry your computer. if you want to mass spawn cubes, the less components assigned to them, the better. I’d even make them static to lower the memory.

If I knew more about what you’re trying to achieve, I can help you more.

  • Causticlasagne.

For simple cubes, creating dynamic vertices and triangles would be the quickest, and require the least resources. It would be faster to render too. You should chunk together multiple cubes into multiple game objects, Minecraft style. Having them all as one model containing every cube may start to push the limits.

Chunking would be your best bet, but testing to find the best chunk size would be needed. A 8 x 8 x 8 cube object sounds like a good start. Chunking would allow for occlusion, so it wouldn’t have to draw every cube every frame. If you need each cube to have a collider, you can add multiple cube colliders to the game object to make a larger compound collider.

You could still color each cube differently if you use vertex colors in you shader. You could use arrays to hold each cube’s attributes (color, size, visible, etc) and have a routine to regenerate a new chunk if any of the cube data changes.