0
0
Unityframework~5 mins

Tilemap basics in Unity

Choose your learning style9 modes available
Introduction

Tilemaps help you build game worlds easily by placing small square images called tiles on a grid.

Creating 2D game levels like forests, dungeons, or cities.
Designing maps that repeat patterns like floors or walls.
Making games where you want to change parts of the world quickly.
Building puzzles or platformer stages with clear grid layouts.
Syntax
Unity
using UnityEngine;
using UnityEngine.Tilemaps;

public class Example : MonoBehaviour
{
    public Tilemap tilemap;
    public Tile tile;

    void Start()
    {
        Vector3Int position = new Vector3Int(0, 0, 0);
        tilemap.SetTile(position, tile);
    }
}

Tilemap is a component that holds tiles arranged in a grid.

SetTile places a tile at a specific grid position.

Examples
Places a tile at grid position x=1, y=2.
Unity
tilemap.SetTile(new Vector3Int(1, 2, 0), tile);
Removes all tiles from the tilemap.
Unity
tilemap.ClearAllTiles();
Gets the tile at position (0,0).
Unity
TileBase currentTile = tilemap.GetTile(new Vector3Int(0, 0, 0));
Sample Program

This script places a 3 by 3 square of grass tiles on the tilemap when the game starts.

Unity
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilemapExample : MonoBehaviour
{
    public Tilemap tilemap;
    public Tile grassTile;

    void Start()
    {
        // Place grass tiles in a 3x3 square
        for (int x = 0; x < 3; x++)
        {
            for (int y = 0; y < 3; y++)
            {
                Vector3Int pos = new Vector3Int(x, y, 0);
                tilemap.SetTile(pos, grassTile);
            }
        }
    }
}
OutputSuccess
Important Notes

Always assign your Tilemap and Tile objects in the Unity Editor before running the script.

Tile positions use Vector3Int where x and y are grid coordinates, z is usually 0.

You can clear or change tiles anytime to update the game world.

Summary

Tilemaps let you build 2D game worlds by placing tiles on a grid.

Use SetTile to add tiles and ClearAllTiles to remove them.

Tile positions are given by Vector3Int coordinates.