I'm trying to create a top-down map in Unity2D, this map will act as an ocean that a 1 tile sized boat will drive across. I want to be able to generate this map programmatically by going through the grid to create shallow areas, spawn things, etc.

I've been reading it's not good practice to create game objects for each tile as there's so many. You can use object pooling but that sounds harder to manage. I've read that using a mesh map is the best way but no one follows up with the best way to implement it. Any clues to push me in the right direction?

  • \$\begingroup\$ Are you refering to procedural TileMap generation? Perhaps an answer from Unity forum related to this topic will help you a little bit. \$\endgroup\$ – Rama Alifiandy Feb 17 '17 at 6:18
  • 2
    \$\begingroup\$ An object ocean is quite a bit deeper than an object pool ;) \$\endgroup\$ – Quentin Feb 17 '17 at 9:39
  • \$\begingroup\$ That many tiles and objects is no problem if you use Spatial Partitioning \$\endgroup\$ – lozzajp Feb 17 '17 at 10:06
  • \$\begingroup\$ Just have a 2 dimensional array filled up with numbers based on what that tile is \$\endgroup\$ – Bálint Feb 17 '17 at 11:23
  • 1
    \$\begingroup\$ I recently wrote an answer about rendering a tiled map on a single quad, using a small (1 pixel per tile) index texture to describe which tiles belong at each position. You might find this useful, as it can drastically cut down on the number of discrete objects you need to manage. \$\endgroup\$ – DMGregory Feb 18 '17 at 3:35

UPDATE: As of Unity 2017.2, Unity now comes with an implementation of the mesh-grid-textured-quad approach. See the Unity documentation on Tilemap, and refer to the techdemo repository here for some examples of custom tiles.

Old Post------ There's a number of ways to do this sort of thing.

One is with Object Pooling. It's useful if you know you'll only show a small number of tiles to the player, but with a potentially huge map. A tutorial by the Unity team on how object poolers work can be found here.

The second way, if you plan to show potentially all of your tiles at a time, is to render them as individual quads on a single mesh. Basically you just swap out the texture mapped to the specific quad in the mesh. This can be more difficult to get started, but is very efficient for large maps that you always want visible (think civilization style). A tutorial on 2d maps built in this manner can be found here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.