2D Toolkit Forum

2D Toolkit => Support => Topic started by: roaet on August 31, 2013, 07:01:05 am

Title: Tilemaps are amazing, but how do I dynamically create them?
Post by: roaet on August 31, 2013, 07:01:05 am
I really like tilemaps but I have level data stored externally and would like to generate them runtime. Is that possible? Thanks for the support. Love your product!
Title: Re: Tilemaps are amazing, but how do I dynamically create them?
Post by: unikronsoftware on August 31, 2013, 12:10:05 pm
Hi,

There isn't an "official" way to create tilemaps at runtime, as a lot of the code is simply stored in editor code to reduce runtime bloat. Easy option - create a HUGE tilemap (max size is fine), but keep it empty. Then you can tweak as necessary at runtime - empty tilemaps don't really take up that much memory (close to negligible).

Or do the above, create a prefab and instantiate at runtime. The tilemap requires the data / editor data objects, which are a pain to generate at runtime.
Title: Re: Tilemaps are amazing, but how do I dynamically create them?
Post by: Pfaeff on September 01, 2013, 04:34:56 pm
You could use
Code: [Select]
tileMap.Layers[i].SetTile(x, y, id)to set the tiles at runtime. You can get the id via
Code: [Select]
tileMap.SpriteCollectionInst.GetSpriteIdByName("YOURSPRITENAME")
At the end, use
Code: [Select]
tileMap.Build()to build the actual tilemap.
Title: Re: Tilemaps are amazing, but how do I dynamically create them?
Post by: pangyan on March 03, 2014, 06:31:58 am
For anyone who is interested, I posted a comprehensive guide to dynamically creating a TileMap here: http://praveenangyan.com/2014/03/02/dynamically-displaying-a-level-with-tk2dtilemap-in-2d-toolkit-for-unity/ (http://praveenangyan.com/2014/03/02/dynamically-displaying-a-level-with-tk2dtilemap-in-2d-toolkit-for-unity/)