Paint levels with tilemaps
Add a tilemap, paint and erase tiles in the Tilemap Editor window, create Rule Tiles that choose their own sprites, and add collisions in Unity Spark.
Read time 4 minutesLast updated 3 hours ago
A tilemap is a grid that you paint sprites onto, one cell at a time. Use tilemaps to create platforms, walls, and floors. For how tilemaps fit into a 2D game, refer to Introduction to 2D games in Unity Spark.
Add a tilemap
To add a tilemap to your scene:
- In the Hierarchy window, select Add object (+) or right-click.
- Select 2D Object > Tilemap.
- Choose one of the following grid shapes.
Shape | Use it for |
|---|---|
| Rectangular | Square cells, such as a side-scrolling platformer. |
| Hexagonal Flat Top | Six-sided cells with a flat top, such as a strategy map. |
| Hexagonal Point Top | Six-sided cells with a pointed top, such as a strategy map. |
| Isometric | Diamond-shaped cells that give a three-quarter view. |
| Isometric Z as Y | Diamond-shaped cells that give a three-quarter view and let you stack tiles in layers. |
To add another layer to an existing hexagonal or isometric tilemap:
- Select its grid object.
- Add a tilemap of the same shape.
Unity Spark adds the new layer to the same grid.
Prepare tiles to paint with
The Tilemap Editor window paints with sprites from textures you sliced, and with Rule Tiles. Before you paint, do one of the following:
- Slice the texture that holds your tiles in the Sprite Editor window. Refer to Slice a sprite sheet.
- Create a Rule Tile. Refer to Create a Rule Tile.
Paint tiles
To paint tiles on a tilemap:
- Select the tilemap object.
- In the Inspector window, select Open Tilemap Editor in the Tilemap component.
- In the Tilemap Editor window, under Source, select the texture that holds your tiles.
- Select a tile. To paint a group of tiles at once with Paint, drag to select them. Rect and Fill always use a single tile. You can also select a Rule Tile from the Rule Tiles list.
- Select one of the tools in the following table.
- Click or drag on the grid.
The following table describes the tools.
Tool | Description |
|---|---|
| Paint | Paints the active tile. Drag to paint many cells. |
| Erase | Removes tiles. Drag to erase many cells. |
| Rect | Fills a rectangle that you drag with the active tile. |
| Fill | Replaces a connected area of matching tiles with the active tile. |
To zoom, use the mouse wheel. To pan, drag with the middle mouse button. Unity Spark saves each stroke as you paint, and you can undo each one. Fill changes up to 2000 cells at a time. If the rectangle you drag with Rect is larger than 2000 cells, Unity Spark doesn't paint it. Drag a smaller area or zoom in.
The Tilemap Editor window shows the grid as a simple diagram. The real tiles appear in the Scene view.
Create a Rule Tile
A Rule Tile chooses its sprite based on the tiles next to it, so edges and corners draw correctly as you paint. A Rule Tile can also pick a random sprite, or play an animation.
To create a Rule Tile:
- In the Project window, select Create (+) > 2D > Tiles > Rule Tile. For a hexagonal tilemap, select Hexagonal Rule Tile instead.
- Select the new Rule Tile to edit it in the Inspector window.
- Select a Rule Type: Neighbour, Random, or Animation.
- Under Source texture, select a sliced texture.
- Set the Default sprite.
- Select + Rule to add a rule.
- In the rule's grid, click each cell to set what the neighboring tile must be.
- Select a slice.
- Select Set to choose the sprite for that rule.
- Select Save Rules.
A Neighbour Rule Tile paints only on a tilemap of its own shape. A square Rule Tile paints on a square or isometric tilemap, and a hexagonal Rule Tile paints on a hexagonal tilemap. A Random or Animation Rule Tile paints on any tilemap.
Add collisions
To make a tilemap solid, add a TilemapCollider2D component to the tilemap object. For how to add a component, refer to Add 2D physics to objects.
The Collider Type of each Tile and Rule Tile asset decides its shape: None, Sprite (the outline of the sprite), or Grid (the whole cell).
To merge the tiles into one collider:
- Add a CompositeCollider2D component to the tilemap object.
- Enable Used By Composite on the TilemapCollider2D component.