Best way to make a tilemap with the 2D features

I’m wondering what is the best way to make a tilemap with the 2D features. (Tilemap for a platformer like an old mario, not top-down like an old rpg, if that changes anything).

What I’ve thought of so far:

  1. There doesn’t seem to be a way to procedurally generate a sprite the same way you can generate a mesh. This would mean that if we’re using sprites we would be using one sprite for each tile. That seems to be inefficient.

  2. So since it would be inefficient to display too many sprites, we could generate a flat “3D” mesh that is tiled the way we want it.

  3. For collision, instead of using a mesh collider, use a PolygonCollider2D and set the “points” to the vertices of the mesh.

Am I thinking correctly? Or am I wrong in my assumption that displaying hundreds of sprites for a tilemap is too inefficient?

How would you guys go about making a tilemap for a platformer with the 2D features?

You are right that using one GameObject with one SpriteRenderer is very, very inefficient. You are also right in that it’s better to generate a mesh to represent all tiles. I wrote a tilemap library for Unity (hosted on GitHub), you can look at how I do it or use the library in its entirety.

Not a very specific answer but it’s a broad question, there are many parts involved in solving this problem.

Well I want to avoid giving you my source code (Because its big, un-finished, and I’m using it for my game) but I can tell you what I did.

Procedural creation of a 3D mesh and a submesh for each unique kind of tile works, I make levels completely out of png files with a pixel color representing a tile in game. I also have like 200 boxcollider2Ds generated on the GameObject via the procedural generation… Kinda sucks, wish I had a better way of doing it. Doesn’t take long to generate though, I pre generate them in the editor and it gets saved off to the side… I didn’t check for exact numbers but it takes less than a second so generation in the beginning of the game is reasonable when I did do it that way. But one of the things I need to do soon is chunking the mesh into pieces so it reduces the time to draw via Occlusion, So its not one big mesh with tiny tiles.

I will make a package for this kind of feature on the asset store after I polish things like collider2D generation and chunking. But its not impossible for someone to make it themselves. Its far better than setting each tile in the editor! If you need help in the way I’m doing it, Contact me!

Honestly (and I’m not trying to dissuade anyone from challenging themselves), but you should look into purchasing the 2D Toolkit asset as it has an extremely robust and efficient Tilemap system built-in and will save you boat loads of time. I know this doesn’t really answer your question, but I had to put it out there in case you weren’t aware.

Not sure how well this would work for your game or if you are still looking for a solution (I’m only 2 years late), but awhile back I came up with a system that I’m reasonable happy with.

Simply create a small grid of 2d box colliders (3x4? 5x5? Depends on character size) that stays centered on the player, but snaps to your tile grid. Then simply enable/disable the colliders depending on whether there is a tile behind them or not. This can save you a lot of time and memory space by only having colliders where they actually matter

Expanding on this, I don’t see it being terribly difficult to swap the box colliders out for other collider shapes that better match your tiles (ramps, half-tiles…). Perhaps using enums for different shapes.

A few drawbacks:

If your map has a lot of players/enemies/npcs, you can still end up with a lot of colliders. It can quickly become less efficient than other methods, especially since characters close together will each have their own collider on the same tile. (Maybe figure out some sort of pooling/sharing?)

Even if your colliders are aligned perfectly, the player can still end up “tripping” on the edges. A quick fix is to actually scale down colliders that are not immediately adjacent to the player ever so slightly. Then return them to normal when the player is on them.

Perhaps I had simply set things up improperly, but oftentimes, my player would pass through one or more layers of tiles when falling or moving fast. Though as I type this, I’m realizing that I probably should have moved the colliders in “FixedUpdate” rather than “Update”. Live and learn…