Generating a TileMap

Hello.

Hopefully this thread won’t be too long, I will divide the different questions in blocks, so if you don’t want to read everything, please just try to answer one :).

  1. I want to generate a 50x50 TileMap using a 2-dimensional array, that has references to the GameObjects.
    I want every tile to have the height and width of 40px ( that’s also the size of the sprite I am using for each tile ).
    When importing the sprite, adding it to a game object ( thus adding a sprite renderer ), it appears as only ~8px wide and tall. Where exactly can I configure, so I see it in the exact size as the actual image?

  2. Now secondly I don’t know the best way to implement the tile map. I read many things and my idea was to create different prefabs and put them in the tilemap array, then generate the map from it. I also need the array to be able to access each tile ( tilemap[×][y] ), otherwise I could of course just place each tile next to each other in the scene, though dynamically creating the tilemap seems like the better solution.
    For a 50x50 tilemap that however means 2500 gameobjects. It won’t be a huge game, but that seems very much. There are basically 3 different prefabs for the tiles ( for my bomberman clone ). A passable tile, a non-passable tile ( that one has a collider ) and a destroyable-tile ( also has a collider on it, though the collider will be removed, if destroyed, so it becomes passable ).

  3. I don’t know how collision works internally, I thought of also dynamically adding the colliders, depending on the character movement. It won’t make much sense to test collision with every other collider on the map ( I don’t know if that is what unity is doing ), for each direction the player walks, there’s only 3 possible tiles, the player can collide with.
    But what’s the best way to create the tilemap? I thought of somehow reusing gameobjects that do the same thing. When I implemented bomberman in java, I was just drawing the tileimages on their positions. Having a gameobject for each tile seems like a huge overhead? But I can’t just make a whole image, since the tiles aren’t static ( they will of course change during the game ). I have for example a gameobject named Tile and that has a gameobject not_passable, that contains the Sprite Renderer. When I want to “draw” things, I need to create a gameobject ( that has a transform ) with a sprite renderer, right?

Thank you guys very much in advance for taking the time. I know this topic ( tile maps ) has been the question of many threads ( I googled a lot about the topic ), but still didn’t answer all my questions.

I think you’re looking for Pixels to Units in the importer, see Sprites in http://docs.unity3d.com/Manual/class-TextureImporter.html

1 Like

Thanks for your answer. First, I made a little mistake. My tile image is actually 16x16 pixel, though when I made this game in Java, I drew it larger ( 40x40 pixel ). What do I have to set the pixels per unit if I want the image to be displayed with its actual size ( 16x16 pixel )? Its set to 100… and is there a way to give it a fixed pixel size in the editor ( since I want it to be 40 x 40 pixel )? I think I could write a script, but I like to have it in the editor with my desired size.

Take a look at this blogpost that Unity published today Unity Blog

1 Like

Thanks again, great article and good that it was posted just in the right time :). That helped me a lot and I think answered at least one of my questions. Now I just have to fully understand the article, though I roughly already did.

Any ideas about the tile map? With 40x40 pixel tiles, I would generate the tilemap and place each tile next to each other with a gap of the tile size ( 40x40 pixel ), right? I guess I do the same thing, if my bomberman game would be 3d ( which I think I will do at some point.

For the tile-map you might need to do some combining of graphics and colliders, but I’d just go with the naïve implementation you describe above first, and then see if it needs optimising on your target platform.

If people are interested, here is my current approach and how I managed to show pixel perfect sprites ( because with pixel graphics you want one unit to correspond to an integer value of pixels, or otherwise put, you don’t want a pixel of your image/sprite to be drawn on a floating number of pixels of your screen).
So the best thing todo was to just have 1 unit represent 1 pixel, the thing with that was, that the size in the editor will be huge and gizmos will appear way too small. Therefore I used 100 PPU on the sprite ( so it’s 1/100 of it’s actual size ), then divide the camera’s size by 100 ( or whatever you put on PPU ) and bingo, everything’s fine :).

For more information the link posted above is great, or this video tutorial: http://captain-andy.com/post/73251769864/episode-2-making-pixel-perfect-units-in-unity3d