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.


  1. Doesn’t the 2D Toolkit already have the ability to load tmx files? Why didn’t you do that? Or is this post older than that capability?


    1. 2D Toolkit has the ability to import .tmx files which you can then edit. But if you prefer the Tiled Editor, or if you have a lot of .tmx files that you don’t want to import one by one, this method will work. Also the method described will help you create a dynamic level. In my game Pocket Tales, I load up a .tmx file and then dynamically randomize certain aspects of the loaded level.


  2. Hey Praveen ,
    I am trying to achieve something like this . Actually I am developing a game like Gravity Guy ( http://www.silvergames.com/gravity-guy ). For this I want to generate levels at run time , actually for this I planning to have around 20 level files which will be laded randomly one after the another.

    Using Tiled and an open source frame work I am able to achieve this but whenever new level is loaded there is lag . Now I just bought 2d Toolkit , so can you please guide me about how I can achieve random level loading without any lag and is it even possible to achieve speed at which platform is generated in Gravity guy by loading XML at runtime.

    Or any other way so that my games platform scrolls without any lag.


Leave a Reply to eklavyaa Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s