Help with top down 2D chunk system

Hey guys I am fairly new to Unity but am studying software engineering at uni and so far I've been having a blast. However, there is a question has been looming ahead for a while now, that is, what's the best method to store/load 2D Tilemap chunks? At this current point, I do have something up and running, but my current system is not an option if I want to use Rule Tiles. Right now I have a chunk class that creates a new Tilemap every time a new chunk needs to be generated, and when I move far enough away from the chunk I just disable the Tilemap Renderer component. It was heavily inspired from Sebastian Lague's series on chunk generation, except in 2D instead. Although this system means there comes to be many disabled gameobjects in the hierarchy :(

However if I want to use rule tiles, then the chunks are separated from each other (as each chunk has its own Tilemap component) and there is no cohesion between them. I have been thinking about a solution to this, but I need some advice on how to proceed. I could just use a single Tilemap for everything, which would solve the issue of cohesion, but then how would I disable the chunk? By setting each tile of chunks I don't need to null and then loading it back again when I need it? Idk it just seems inefficient. Furthermore, I have no idea on how to store all the details of each chunk into a seperate file for saving and loading as all I have been doing is disabling the tile maps. I would like to eventually implement that unless there is a better option for saving/loading? I'm planning for my procedurally generated map to be around 10000 tiles width and height, but I obviously can't load (or generate?) that all at once.

Just thought I would attach a screenshot of the current state of my game for help envisioning what I'm talking about, although apologies if I didn't make any sense at all. I am more than happy to elaborate on anything. So far rule tiles are only implemented on the sand, as I thought I would fix this chunk system before I even begin on the water.

Thanks :)5388744--546402--With Cohesion.jpg5388744--546405--Without Cohesion.jpg

For efficiency I would advise you to look at circular array, in those cases you want a 2d version. That's how old console handled their scrolling too. For example sonic used 256x256 metatiles (chunk) to make its level, they copied into the vram tilemap.

I guess it is pretty late for a reply and I am not an expert but why don't you reuse your disabled tilemaps?
Lookup a pulling system here is a video that explains the concept. So whenever the player goes out of range of the visible tilemap just have some code that moves that tilemap in front of the player (in the direction the player is moving) and populate it with the data set of the chunk that is supposed to be in that location. That way you will always end up with a set copy of tilemaps that you rotate.

I think that is what @neoshman was proposing. Just overriding existing chunks.
I also think that having a single tilemap and removing the tiles you do not need to display is probably better for performance. Sadly I am now in the process of researching the topic so I am not sure if that is the best option.
Here is an example of how to clear your tile data. Just set tiles on of your tilemap to null with the setTile(position, null) method.

I actually came here to find a solution to the same problem you have. I want to build a chunk system for my tile maps.

Your game screenshots look amazing. Keep up the good work.

Lol it's been quite a few years but I thought I would share what I ended up doing. I got it working a while ago and most importantly it's super performant!

Essentially, @Nemezis is correct. Having one Tilemap for ALL the chunks and then setting/removing tiles is a much better option. I originally did not do this because I was concerned about calling SetTile too much...which would cause my game to freeze every time a chunk was loaded/unloaded. But there was a simple fix unbeknownst to me at the time. Use SetTilesBlock instead!

So to load a chunk, call SetTilesBlock with the array of tiles you want to load. To unload a chunk, call SetTilesBlock with an array of null tiles. Simple!
9056878--1251946--Screen Shot 2023-06-05 at 9.10.06 am.jpg

1 Like