Hello Guest

Author Topic: Can you use the tilemap without rendering the tiles?  (Read 8361 times)

1337Rooster

  • Newbie
  • *
  • Posts: 9
    • View Profile
Can you use the tilemap without rendering the tiles?
« on: September 03, 2013, 06:46:56 am »
Levels in my game would involve stringing multiple tilemaps together (At certain points I load up the next tilemap in the level by spawning in a bunch of objects when the player completes an objective). You could say that levels are a series of segments, each containing a tilemap, an objective and perhaps other options. So what I really want is a visual way to define a tilemap for a segment, but provide that as data to my game (Rather than just loading that as a full scene). Then have my code be able to take that data and do what it wants with them (I.e. when I place them in the grid I want my GirdManager object to know about them). So the thing I want to gain from the tile map is basically a nice visualization to setup grids, which are parts of  the levels. Each time I load a new tilemap up, there may be a new objective for the player, I would like to define a few properties for this as well.

Levels would consist of:
- A series of options (Maybe I'll have some extra XML/JSON configuration in which will define these options and point to a tilemap).
- multiple tilemaps


An alternative approach I am looking at is using Tiled. As this exports JSON/XML for the levels and I can load this up and manually instantiate prefabs from this. But I need a lot of code to map the JSON to my prefabs.

I've noticed that the tk2d tile map will create a game object with a bunch of prefabs at the painted locations. And the tk2dTileMap  API here doesn't seem to let you iterate over the tiles in the map. It seems like it just instantiated a game object, which contains all of the 
http://www.unikronsoftware.com/2dtoolkit/docs/2.10/html/classtk2d_tile_map.html

So maybe the way to do this with the t2kd editor would be to define a tilemap for each segment of my level, and generate the TileMap Render Data (GameObject hierarchy), store that in a prefab and instantiate one of those when I want to load them. Then iterate across the GameObjects hierarchy and do whatever extra work I need? I'm wondering what the best practice is here? This is essentially the only way to get at this data, correct? Take the prefab the tilemap generated and store that.

Another question, does the TileMap GameObject still need to be around? Or just the TileMap Render Data GameObject hierarchy it creates when you hit commit?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can you use the tilemap without rendering the tiles?
« Reply #1 on: September 03, 2013, 03:09:07 pm »
If you're going to do this I strongly reccomend writing some code to convert a tilemap to a static sprite batcher. Its pretty straightforward - just loop through the tiles, get x, y and a position for the tile, and add a sprite to the static sprite batcher. It is much more efficient to spawn static sprite batchers at runtime and a lot easier to manage as well.

The tilemap system is optimized for a large world, but not for having multiple tilemaps in the scene. It can be done, but its going to be a pain as you'll be working against the system.

Once you have a simple system to convert to a static sprite batcher, you should be able to simply save them as prefabs and instantiate as needed - the static sprite batcher is just one object - no render data, etc to deal with.

1337Rooster

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Can you use the tilemap without rendering the tiles?
« Reply #2 on: September 04, 2013, 07:03:00 am »
I'm not sure if that solution fits my use case very well. I still want to be able to instantiate prefabs from the sprites, which is something the TileMap offers directly, but the tk2dStaticSpriteBatcher does not, if I understand correctly. The best I could do then is use the tk2dStaticSpriteBatcher prefab as a sort of data structure which can vend me a spriteId that I'll need to process and use custom code to load a prefab.

After playing around with the tile map editor, I do find it a bit more difficult to use than Tiled, but its pretty good and is well integrated with unity. It seems very good for certain types of games. Mostly because its hard to get it to work well with the scene view, I do a lot of zooming around, and I don't think there is a way to show gridlines for the tilemap (Or I haven't been able to find the option). However, importing a .tmx from tilemap seems to work pretty well as long as you construct a tilemap with the same indicies for sprites that you made in Tiled.

So if I do use the TileMap I would probably make it in Tiled and import it in. But then I figure I may as well just do everything in tiled, and have my game read the .tmx files and process them. Then when I want to load a new "Segment" into my game, I can load up a .tmx file and iterate over it to instantiate the Prefabs. I don't really see the benefit of using the tk2dSpriteBatcher as an intermediate data structure.

Also, I think the tk2dBatchedSprite is missing from the scripting reference.

Thanks for the help though, I can see why you would suggest the StaticSpriteBatcher and it would make sense, if I didn't need to have prefabs to go with the sprites.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can you use the tilemap without rendering the tiles?
« Reply #3 on: September 04, 2013, 12:49:18 pm »
The static sprite batcher doesn't do prefabs like tilemaps, but you can attach them as children to the static sprite batcher.
If you want to do everything in tiled and read tmx files, it will be more efficient to construct a static sprite batcher out of the result.

Batched sprite is missing from the script reference, intentionally - there is a very small subset of documented and public API, but its more than enough to do what you need to here.
http://unikronsoftware.com/2dtoolkit/doc/2.20/advanced/scripting_static_sprite_batcher.html

The advantage with the spritebatcher is that its WAY faster than instantiating the same number of loose sprites. Eg. if you build a section out of 20x20 sprites, creating 1 batcher with all these sprites in will be significantly (and noticably) faster than creating the 400 sprites you'd need for that section. It makes sense to use it where you can.