How to reduce the number of gameobjects used for representing building blocks

I’m developing a 2D building game for mobile devices where it is possible to spawn building blocks by touching the screen. At the moment each of those blocks is represented by a gameobject that has a sprite renderer and a physics collider attached. The problem is that I need up to 20000 of these blocks in the scene at the same time. That might sound a bit much but there are some restrictions that ensure that the game still performs pretty well. So my problem isn’t performance related in terms of physics or rendering, it’s just the mere number of gameobjects that gets me into trouble. It doesn’t matter if I use my building blocks or just empty gameobjects, after instantiating several thousands of them every additional instatiation takes forever.

For that reason I’m trying to cut down on the number of gameobjects used in my game by combining some blocks into bigger gameobjects. Unfortunately I can’t figure out how to get this right.

I read that in Minecraft-like games several thousand blocks are combined into chunk meshes (using one mesh collider) that can be rebuilt whenever a block is deleted but as far as I can tell that is not possible using sprites. Also I think it might not be sensible to follow that approach since my blocks are not necessarily aligned next to each other and therefore there would have to be some holes in the mesh.
Has anybody tried to achieve something like this, or do you have any ideas on how i might approach the problem differently?

Any suggestions would be appreciated!

This is one scenario where I’d recommend addressing efficiency from the start, since this will end up being a fundamental design issue, not simply an optimization issue. 20,000 blocks in a scene will never be practical. Here are some suggestions:

  1. Combine blocks. If you have a 3x3 pattern of blocks, replace it with a single 3x3 GameObject. If you need to break them apart at some point, replace them with the individual blocks only at that point.
  2. Use space partitioning, such as a quadtree, and only spawn GameObjects within the player’s immediate area. I write “spawn” instead of “instantiate” because you should use object pooling for this. So the full map will be in memory (an abstraction), not in the scene.

Also, as a final suggestion, consider posting open-ended questions like this on the Unity Forums instead of Answers. In Answers, you’ll get better responses for cut-and-dry questions. The Forums are better for discussions.