Tilemaps and Level Design
Hand-placing hundreds of individual sprite GameObjects for a level doesn't scale. Unity's Tilemap system lets you paint a level like a grid-based canvas instead.
Setting up a Tilemap
- Right-click in the Hierarchy → 2D Object → Tilemap → Rectangular, this creates a
GridGameObject with aTilemapchild. - Open the Tile Palette window (Window → 2D → Tile Palette), create a new palette, and drag your tile sprites into it.
- Select the paint brush tool and click on the Tilemap in the Scene view to place tiles.
Making the tiles solid
On the Tilemap GameObject, add a Tilemap Collider2D and a Rigidbody2D set to Body Type Static. A TilemapCollider2D automatically generates a collider shape from whatever tiles are painted, so painting a new platform is enough, no manual collider work needed.
Reading tile data from code
using UnityEngine;
using UnityEngine.Tilemaps;
public class LevelInfo : MonoBehaviour
{
public Tilemap tilemap;
public bool IsSolidAt(Vector3 worldPosition)
{
Vector3Int cell = tilemap.WorldToCell(worldPosition);
return tilemap.HasTile(cell);
}
}
The pieces
Griddefines the cell size and layout that every childTilemapshares, a level can have several Tilemaps (ground, background decoration, hazards) stacked on the same Grid.- A Tilemap's
Rigidbody2Dshould beStatic, the level geometry doesn't move. WorldToCellconverts a world-space position (like a GameObject'stransform.position) into the Tilemap's grid coordinates, useful for gameplay logic that needs to know "what tile is under this point."
TIP
Keep hazards (spikes, lava) on a separate Tilemap from solid ground, with its own collider set to Is Trigger. That way a single OnTriggerEnter2D check on the player can detect "touched a hazard" without it also being solid ground.