Hello Guest

Author Topic: Instantiate a prefab relative to the tilemap grid? How do you do it?  (Read 5013 times)

robutmike

  • Newbie
  • *
  • Posts: 10
    • View Profile
Hello fine folks.

I am trying to figure out how to instantiate prefabs based on the grid of the tilemap. Right now I can set tiles at a particular X and Y in the tilemap using Tilemap.SetTile but I can't put prefabs into my map at positions relative to those coordinates.

So for example, lets say you have a 10 tile by 10 tile room for your platformer game map. Its totally open in the inside with a wall all the way around it. I want to put a prefab "player" object or "spikes" object etc in the room. I can't add the prefab TO the tilemap obviously, as its not a tile. However, I do want to place it in the room, without resorting to figuring out specific pixel distances if possible.

The only alternative I have found so far is to use the size of the tiles in pixels and then do some crazy multiplication mess, but I am thinking there must be an easier way.

So tell me how you do it (or how you WOULD do it).

Thanks!


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Instantiate a prefab relative to the tilemap grid? How do you do it?
« Reply #1 on: January 16, 2015, 01:53:51 pm »
You cna use the prefab system in the tilemap with some placeholder tiles to spawn prefabs automatically. Load the tilemap sample and go into edit mode to see what its doing in there - the coins are placed as tiles but spawned as prefabs when committed.

robutmike

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Instantiate a prefab relative to the tilemap grid? How do you do it?
« Reply #2 on: January 23, 2015, 04:47:37 pm »
Thanks! That helped immensely.

BUT! If I want to spawn a prefab at that position that is more than 1 tile in size (say... 2 tiles tall and 1 tile wide) would that still work? Or would I need to spawn it directly at that coordinates using the getileFrac method?

EDIT: I meant specifically tileMap.GetTileFracAtPosition

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Instantiate a prefab relative to the tilemap grid? How do you do it?
« Reply #3 on: January 23, 2015, 05:05:48 pm »
GetTileAtPosition gets you the position of a tile, depending on the anchor point of your larger sprite you may need to position it based on the bounds. That really depends on what you're trying to achieve specifically.

The prefab system simply spawns a prefab at a position -  they can be 2x the size of a tile when committed...

GetTileFracAtPosition returns the fractional tilemap position, not sure how that is relevant here?

robutmike

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Instantiate a prefab relative to the tilemap grid? How do you do it?
« Reply #4 on: January 31, 2015, 03:59:02 pm »
Thanks UKS!

Basically I am making a random dungeon generator. I know how to do this a few ways, but am exploring additional options with TK2D and think it would be really convenient to use the features I love from TK2D.

What if I wanted to build several prefab dungeon rooms made of tiles and load them into the scene at random? I know how to do this with a loop, but it becomes rather tedious setting each individual tile for the various room types. I was wondering if you have any suggestions on how I could make the rooms beforehand (as prefabs) and then load them into the scene when the map is generated (assume I generate the entire map when the player comes into the dungeon). Can I somehow load them into one tilemap? Do they all need their own separate tilemaps that I will then have to align? Will having 20-30 small tilemaps at once be too computationally expensive vs one large tilemap?

Thanks again for all your help,
Mike

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Instantiate a prefab relative to the tilemap grid? How do you do it?
« Reply #5 on: February 02, 2015, 12:13:18 am »
20-30 small tilemaps shouldn't be noticably different to one large one.
In this case I'd lean towards doing what is easiest / quickest to implement.