2D Toolkit Forum

2D Toolkit => Support => Topic started by: Pfaeff on January 30, 2013, 10:24:40 pm

Title: TileMap from Code
Post by: Pfaeff on January 30, 2013, 10:24:40 pm
Hi there,

I really like the tilemap editor, which inspite of the fact that it's still in beta is very easy to use and powerful. However, I would like to create a tilemap using a script. This would happen at loading time, so it doesn't have to be very fast. From this point on, the map will be pretty much static.
Is there a way to manually pass data to the tilemap object and tell it to create the tilemap render data? The reason I attempt to do this, is because I want to benefit from the optimized collider construction done by the tilemap and also to be compatible with maps that are created entirely by hand.


Thank you very much,
Pfaeff
Title: Re: TileMap from Code
Post by: unikronsoftware on January 30, 2013, 11:29:07 pm
Yup.
Set up the tilemap as you would normally, but don't bother painting anything in. Still have the spritecollection, etc set up though. Also, set up the layers you'd like to use and the size, etc.
Get a reference to it.

public tk2dTileMap tileMap;
tileMap.Layers[0].SetTile(x, y, spriteId); // get spriteId from your sprite collection
...
// when you're done messing about with the contents, call
tileMap.Build(); // to build geometry & colliders
Title: Re: TileMap from Code
Post by: Pfaeff on January 31, 2013, 10:01:40 am
That is awesome. Thank you very much :).

Is this something that can be found in the script reference? I had no luck searching for it unfortunately  :-\.
Title: Re: TileMap from Code
Post by: unikronsoftware on January 31, 2013, 11:11:29 pm
No, I haven't got around to documenting it. Well it was because I thought I was gonna be changing all that, but I've grown kinda fond of the interface now...
Title: Re: TileMap from Code
Post by: CntrySocialite on February 03, 2013, 06:06:21 am
Instead of setting tiles one by one is there a way to use the "import tmx" method with the described code above?
Title: Re: TileMap from Code
Post by: unikronsoftware on February 03, 2013, 12:40:50 pm
No, it is not enabled at runtime and for good reason.

The runtime import code uses System.Xml (adds significant bloat to your .net runtime), and requires a zlib external assembly (adds more bloat). The code in there is pretty straightforward to turn standalone, just keep in mind how much bigger your game is going to get because of this.

You will be much much better off if you can convert the tmx into a format which loads quicker / easier into Unity, perhaps some serialized structure.