Hello Guest

Author Topic: Tilemaps are amazing, but how do I dynamically create them?  (Read 4218 times)

roaet

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Tilemaps are amazing, but how do I dynamically create them?
« 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!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemaps are amazing, but how do I dynamically create them?
« Reply #1 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.

Pfaeff

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Tilemaps are amazing, but how do I dynamically create them?
« Reply #2 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.

pangyan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Tilemaps are amazing, but how do I dynamically create them?
« Reply #3 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/