Dynamically displaying a level with tk2dTileMap in 2D Toolkit for Unity

2D Toolkit has a powerful level (tile map) editor, but sometimes when you need to use a different format for tile maps like Tiled (.tmx) or you want to create randomly generated levels, you’ll find that you need to dynamically display the level from scratch. The game that I am creating called Pocket Tales uses Tiled and also creates randomly generated levels, so I had to figure out how to dynamically display a tk2dTileMap using the 2D toolkit. I’m not going to describe how to parse a Tiled (.tmx) file, but if you some example code on parsing a Tiled (.tmx) file, please take a look at TurnItUp, an open source library for creating turn based Roguelikes and RPGs that has basic support for Tiled files.


  1. Create an empty tile map in the editor with the maximum size that you anticipate your level being. Instructions on creating a tilemap in 2D Toolkit can be found here.
  2. Create an additional tile map in the editor for each tile set that your level uses. Note: 2D Toolkit’s tile map limit you to only one tile set per tile map.
  3. For each tile map create as many empty layers as you think you will need. Note: I use 4 layers in total for all my levels, one for the background, one for the obstacles, one for interact-able objects and finally the top most layer for the enemies and the player. This essentially means I create 3 layers for my first tile map which uses a tile set for the level graphics and 1 layer for the second tile map which uses a tile set for the enemies and the player.

Code to dynamically display level

I created two empty 2D Toolkit tile maps called BoardTileMap and CharactersTileMap with 3 and 1 layers respectively.

tk2dTileMap boardTileMap;
tk2dTileMap charactersTileMap;

boardTileMap = GameObject.Find("BoardTileMap").GetComponent();
charactersTileMap = GameObject.Find("CharactersTileMap").GetComponent();

// Then use SetTile to dynamically set the tile based on the input format, whether it is another tile map format like Tiled(.tmx) or even randomly generating the level

charactersTileMap.SetTile(0, 0, 0, 1);
charactersTileMap.SetTile(3, 2, 0, 3);

// Finally call Build() on both the tile maps



  • Overlapping tiles when using pixels per meter with a value other than 100 The tk2dCamera has a setting called pixels per meter. Sometimes you’ll find that you get overlapping tiles in the TileMap if you use a setting other than 100 pixels per meter. I just left this setting alone.
  • Imported and displayed tiles are NOT the same color as the originals This is a problem with the way you’ve imported your tile sets into Unity. Take a look at the imported tile maps in the editor and make sure you adjust the import settings to get it to look right first.
  • Tiles are blurry and not pixel perfect This is because Unity has applied a filter to your imported tile set, usually bilinear filtering by default. Click the original image used for the tile set and make sure that you set the Filter Mode to Point.
  • Different tiles are being displayed other than what you expect Keep in mind that 2D toolkit’s tile ids start at 0 and 2D toolkit uses -1 to denote an empty tile. Other formats might use a different system. For instance Tiled uses 0 to denote an empty tile and tile ids start at 1.
  • Level looks flipped or tiles are placed at the wrong location Keep in mind the origin of 2D Toolkit vs other formats. For instance the origin for Tiled is top left whereas 2D Toolkit’s origin is bottom left.
  • My second tile map doesn’t seem to be displayed or seems to be displayed underneath my first tile map This is a sorting issue. There are various ways to get around this, but personally I found that using a separate sorting layer in Unity (introduced in Unity 4.3 specifically for 2D games) for the tile map that I needed to go on top worked well.