2D procedural generation: Tilemaps Vs. GameObjects

I started a project about a week ago of Terraria / Starbound-like game with procedurally generated side-scrolling world, and when it came to generating 2D worlds, there were two major options: Tilemaps and GameObejcts.

I started my project with tilemaps since they looked like the “right” option. A perfect perfect grid where all tiles snap into place and no clutter in the hierarchy with up to dozens of thousands of game objects was pretty convenient if I do say so myself.

But then the problems started coming out:

· For starters, as far as I know, there is no way to split these tiles into regions to manage them as chunks.
· There was also no information on how to unload tiles when out of camera (Occlusion culling) and meant that the whole map would have to be entirely loaded at one, defeating the whole purpose of choosing tilemaps over single-game objects for performance

If anyone has some experience regarding 2D procedural generation or tilemaps in general, it would be immensely helpful if you could let me know if such things (Chunk system and occlusion culling) can be done on a tilemap and how, or which option is better.

Thanks in advance for any contributors, your help is much appreciated :)

If the world gets so big that loading the entire tilemap is a problem, that’s probably when you go to a system of overlapping rectangular tilemaps laid out in a grid, and some type of manager that keeps the useful ones in and destroys the ones that get too far away.

You can tell where an orthographic camera can see pretty easily.
Camera.main.ScreenToWorldPoint
Given a screen point you can obtain world points.

You can also get the grid/tilemap cell of a world point position pretty easily too.
grid.WorldToCell or tilemap.WorldToCell

From this you can figure out what regions are nearby the camera by translating the corners of the screen from screenspace to worldspace to cellspace.

For instance, a very simplified system might divide up the world into RectInt cell coordinate regions, say 40x40 or something. Then it measures what the camera can see. When the camera gets near an unloaded region, it loads that region, setting a tilemap or spawning in new tilemaps. In the same vein, the same regions if not seen for a while would be unloaded.

1 Like