So I am trying to figure out how to create a procedural terrain generation engine for a 2D game similar to Terraria or Starbound. But I am running into a problem when it comes to generating blocks. Apparently I am generating so many that Unity is having a heart attack and dying. So let’s cover what I have so far:
public int mapWidth, mapHeight;
GameObject[,] blocks;
public GameObject baseBlock;
Where baseBlock is a prefab game object with a 2D box collider and spriterenderer attached, with a default sprite. I found that if I place it within the scene but positioned at Y 10,000 so it’s out of site it greatly shortens map generation time because the base block has already been instantiated when I start cloning it.
Then I test generation using the Start() method, generating those blocks is also straight forward:
blocks = new GameObject[mapWidth, mapHeight];
for(int x = 0; x < blocks.GetLength(0); x++)
for(int y = 0; y < blocks.GetLength(1); y++)
blocks[x, y] = (GameObject)Instantiate(baseBlock, Vector2.zero, Quaternian.identity);
That all works just fine save for one problem. If my mapWidth or mapHeight > 256 or so, Unity croaks. Too many Game Objects I guess. I looked into pooling and stuff but that seems unreasonable, requiring the introduction of systems meant for 3D being introduced to your 2D world to create a mesh, etc., etc. My large maps will be 8192x2048 blocks in size, or so I am hoping, so the measly 256x256 blocks I can create right now is a far cry from my objective. What can I do to resolve this?