- Home /
Best way to handle large tilemap?
Hi everyone,
I need your help to understand the best way to build a large tilemap.
Baseline: I'm trying to build a top down 2d game with procedurally generated tile map. The map size would be around 8000x8000 tiles. There can be several characters at the map and you can switch between them. But generally the whole map will never be loaded or visible to a player - only a small area like 50x50 tiles.
Approaches: So I figured out the best way to implement this is to divide map into chunks and only load some of them at a time. However I'm struggling with the best way to render it.
I know having a mesh per chunk is a pretty good and fast solution. But I'm afraid there's going to be some problems around mesh+tiles ordering and sorting.
So I thought maybe using unity built-in Tilemaps per chunk would be fine as well? I may be wrong, but I think they are building mesh under the hood as well, so it should be as good as building my own mesh? If someone got hands on this, can you confirm please?
Thank you!
Answer by Rhyusaky · Sep 15, 2020 at 02:22 AM
Yes you can use Unity tilemaps solution. It will work very fine for entire map loading too (but with some memory impact). The one of uniques drawbacks of this method is that Unity tilemaps doesn't have any mesh optimizations (like using quadtrees for lowering vertices count), but for the great majority of the cases, it will be very fine. Another advantaje of Unity tilemaps is the update velocity, if that was important for you.
How would you handle a map that's defined as 8000x8000 but only renders as 50x50 using Unity? The original question seems to indicate that the game would never allow more than 50x50 tiles to be seen on-screen. If you have a resource file that contained 8000x8000 tiles, how would you instantiate offscreen tiles to handle loading (as destroying) as the camera moves across the tilemap? I have yet to see any Unity tilemap examples for this scale, but I'm genuinely interested as is (or was) the poster.
That's sounds not like an extremely difficult thing to do. In reallity, you can make a script that spawns tiles around player, like a chunk system, it will work very fine I guess.
And that will come with the little advantage of reducing the meshes size at memory in runtime.
Creating a sprite instance in front of a player or behind a player is pretty easy. Destroying objects far away is easy too. But, it seems like it's a grey area in how tilemaps are managed... Does Unity automatically create and destroy objects off screen? I haven't seen anyone really touch on how the tilemap can be managed to prevent overuse of memory...
Answer by KonjiaStudio · Feb 06 at 05:38 AM
Just save the chunk data (Terrain, Objects, Etc.) When they are out of view or chunk visible range. On the tilemap you can set the tile(s) to null when they're out of range and load them back in using the saved chunk data when in range. Using scriptable objects for the tiles works great because you're not allocating new memory for each tile, you are simply referencing the scriptable objects data. I used this method of rendering in an experimental tilemap world generation project I worked on in the past. I was having issues with blending tiles in a mesh, so I tried a simpler approach. It worked fine; it was even better with the unity 2D lighting system which was causing a major headache when having multiple tilemap layers. I hope I helped a bit.
I also recommend you check out the 2DExtras package. The rule tiles work great with the terrain, walls or mountains (Like rimworld). You can plug rule tiles in as if they were normal tiles in Tilemap.SetTile(Vector3Int, Tile).
Your answer
Follow this Question
Related Questions
Draw 2d map array with SetTile or Instantiate ? 0 Answers
How to assign individual data to a tiles? (Unity3D Tilemap) 0 Answers
How to add tilemap to procedural generation 0 Answers
How to return tile based on heightmap efficiently? 0 Answers
Best way to make a big 2D map as smooth as possible? 1 Answer