Hello Guest

Author Topic: Tilemap sprite doesn't display when prefab is attached  (Read 6486 times)

Dayman

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Tilemap sprite doesn't display when prefab is attached
« on: February 01, 2013, 01:54:11 am »
Hi, I'm having some trouble with the tilemap feature in 2D Toolkit. I've got a scene set up with a Tilemap that's working fine when it comes to just displaying the tiles, however I run into trouble whenever I try to use the Data tab in the editor to add a prefab to a tile. When I add a prefab (a collider and a simple script, so the game knows what sort of surface the character is running on) the sprite is visible in the editor, but disappears at runtime--so the prefab is created in the scene, but the tile is invisible because it's only a collider.

I can probably fix the behavior by adding the tile graphic to the prefab as a tk2dSprite component, but I wanted to make sure that this is intended behavior and that I'm not doing something wrong before I proceed.

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #1 on: February 01, 2013, 05:00:06 pm »
You'll need a sprite on the prefab to do what you want.
This is intended behaviour, so you can do funky stuff like turn a tile into a 3d object, etc  if you want.

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #2 on: June 26, 2013, 10:30:18 am »
Is there any other way to achieve this and still have the sprites combined into a single mesh?

I am basically looking to spawn individual collide boxes for each tile (the prefab attach system is awesome) but want the sprite visuals to stick around and be combined into a single mesh.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #3 on: June 26, 2013, 10:33:11 am »
No, there isn't at the moment. but there might be a better way of doing what you want - what is it exactly you're trying to do here?

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #4 on: June 26, 2013, 11:06:11 am »
At the moment I am testing for a tile-based tactics game. I have a set of ground tiles (painted with the tilemap editor) and want each tile to be it's own object with it's own box collide and script (but have the visuals that I painted combined into a single mesh), the aim being to easily pick individual tile objects with touch and send messages to them for highlighting etc.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #5 on: June 26, 2013, 11:25:16 am »
You can work out exactly what tile you've picked much much more efficiently without colliders.
1. You need a world position on the tilemap. You can get by raycasting to the tilemap plane.
2. Use tilemap.GetTileIdAtPosition to get what tile (sprite) is at a particular position. Depending on what you need you could use one of the variants of that function, eg. to get the x, y coordinate of a tile in the tilemap, etc.

If you do GetTileIdAtPosition, all you need to know is what tile ids have associated colliders (you can use GetTileInfoForTileId and the data set up screen for this too).

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #6 on: June 26, 2013, 01:45:32 pm »
I was thinking ease of prototyping rather than efficiency, so was just going to use Easytouch to return the object you tap on. :)

But yeah probably best to do it properly - I can get the world position from the Easytouch screen point via a plane raycast as you suggest. So that gives me the XY of the tile and the type of tile.

For highlighting tiles (showing pathing/attack ranges) - I am thinking another layer above the tiles I have, I can use the same XY and use SetTile on this layer to swap between showing/hiding various overlays. Sound a reasonable method?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #7 on: June 26, 2013, 01:48:49 pm »
Yup that sounds fine. Rebuilding the tilemap, performance wise, is decent, as long as you don't mess about with colliders and prefabs.

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #8 on: June 26, 2013, 01:58:16 pm »
Cheers, so I just call Build() on the tilemap after I've made my changes? Nothing layer-specific? Would it be better maintaining a second tilemap rather than a layer since layer 0 won't be changing?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #9 on: June 26, 2013, 02:00:02 pm »
Just Build will do. It only builds partitions in layers that have changed, so no real need to split it up :)

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Tilemap sprite doesn't display when prefab is attached
« Reply #10 on: June 26, 2013, 02:08:21 pm »
Heh excellent, thanks. :)