unity-tilemap-2d

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity 2D Tilemap

Unity 2D 瓦片地图

Author and script tile-based 2D levels in Unity 6 with the
Grid
/
Tilemap
system, the Tile Palette, colliders, and runtime painting. Targets Unity 6 (6000.0 LTS).
Package note: the core Tilemap (
Grid
,
Tilemap
,
Tile
,
TilemapCollider2D
) is built in. Rule Tiles, Animated Tiles, and Tile Palette brushes live in the separate "2D Tilemap Extras" package (
com.unity.2d.tilemap.extras
)
— install it via Package Manager before using
RuleTile
.
借助
Grid
/
Tilemap
系统、Tile Palette、碰撞器和运行时绘制功能,在Unity 6中创作并编写基于瓦片的2D关卡脚本。适配**Unity 6 (6000.0 LTS)**版本。
**包说明:**核心Tilemap功能(
Grid
Tilemap
Tile
TilemapCollider2D
)已内置。Rule Tiles、Animated Tiles和Tile Palette画笔位于独立的"2D Tilemap Extras"包(
com.unity.2d.tilemap.extras
)中
——使用
RuleTile
前需通过Package Manager安装该包。

When to use

适用场景

  • Use when laying out a 2D level by painting tiles, setting up a
    Grid
    +
    Tilemap
    , adding collision to the map, using auto-tiling Rule Tiles, or generating/editing tiles from script.
  • Use when scenes contain a
    Grid
    with
    Tilemap
    children, or
    *.asset
    tile/palette files.
When not to use: level design practice (pacing, blockout, layout principles) →
level-design
. 3D tile/grid placement → Unity's own 3D tooling (ProBuilder / grid brushes; no dedicated skill here). The platformer character that moves over the tiles →
platformer
/
unity-physics
.
  • 适用于通过绘制瓦片布局2D关卡、设置
    Grid
    +
    Tilemap
    、为地图添加碰撞、使用自动瓦片的Rule Tiles,或通过脚本生成/编辑瓦片的场景。
  • 适用于场景中包含带有
    Tilemap
    子对象的
    Grid
    ,或存在
    *.asset
    瓦片/调色板文件的场景。
**不适用场景:**关卡设计实践(节奏规划、区块搭建、布局原则)→ 参考
level-design
。3D瓦片/网格放置→使用Unity自带的3D工具(ProBuilder/网格画笔;此处无对应专属技能)。在瓦片上移动的平台角色→参考
platformer
/
unity-physics

Core workflow

核心工作流程

  1. Create the grid: GameObject → 2D Object → Tilemap → Rectangular. This makes a
    Grid
    with a child
    Tilemap
    (+
    TilemapRenderer
    ). Use one Tilemap per layer (background, ground, foreground) and set each renderer's sorting.
  2. Open the Tile Palette (Window → 2D → Tile Palette), drag in a sliced sprite sheet to create
    Tile
    assets, then paint with the brush tools.
  3. Add collision to the solid layer:
    TilemapCollider2D
    . For one merged collider (far cheaper and gap-free), also add
    CompositeCollider2D
    (+ a
    Rigidbody2D
    set to Static) and enable Used By Composite on the tilemap collider.
  4. Auto-tile with Rule Tiles (2D Tilemap Extras) so edges/corners pick the right sprite automatically instead of hand-placing every variant.
  5. Edit at runtime via script with cell coordinates (
    Vector3Int
    ):
    SetTile
    ,
    GetTile
    ,
    SetTilesBlock
    , converting world↔cell with the
    Grid
    /
    Tilemap
    .
  6. Verify in Play mode: confirm collisions (Physics Debugger), sorting order, and that
    RefreshAllTiles
    ran after bulk edits.
  1. **创建网格:**依次点击GameObject → 2D Object → Tilemap → Rectangular。这会创建一个带有子对象
    Tilemap
    (+
    TilemapRenderer
    )的
    Grid
    。为每个图层(背景、地面、前景)使用一个Tilemap,并设置每个渲染器的排序层级。
  2. 打开Tile Palette(Window → 2D → Tile Palette),拖入切片后的精灵图集以创建
    Tile
    资源,然后使用画笔工具进行绘制。
  3. 为实体图层添加碰撞:添加
    TilemapCollider2D
    。若要合并为单个碰撞器(性能更优且无间隙),还需添加
    CompositeCollider2D
    (+设置为
    Static
    Rigidbody2D
    ),并在瓦片地图碰撞器上启用_Used By Composite_选项。
  4. 使用Rule Tiles(2D Tilemap Extras)实现自动瓦片,使边缘/角落自动选择正确的精灵,无需手动放置每个变体。
  5. 通过脚本在运行时编辑:使用单元格坐标(
    Vector3Int
    )调用
    SetTile
    GetTile
    SetTilesBlock
    方法,通过
    Grid
    /
    Tilemap
    实现世界坐标与单元格坐标的转换。
  6. 在Play模式中验证:确认碰撞(使用Physics Debugger)、排序顺序,以及批量编辑后是否执行了
    RefreshAllTiles

Patterns

模式示例

1. Painting tiles at runtime (cell coordinates)

1. 运行时绘制瓦片(单元格坐标)

csharp
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilePainter : MonoBehaviour
{
    [SerializeField] private Tilemap tilemap;   // assign the target Tilemap
    [SerializeField] private TileBase groundTile;

    // World position -> cell, then place a tile.
    public void PaintAt(Vector3 worldPos)
    {
        Vector3Int cell = tilemap.WorldToCell(worldPos);
        tilemap.SetTile(cell, groundTile);
    }

    public bool IsSolid(Vector3 worldPos)
        => tilemap.GetTile(tilemap.WorldToCell(worldPos)) != null;
}
csharp
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilePainter : MonoBehaviour
{
    [SerializeField] private Tilemap tilemap;   // 赋值目标Tilemap
    [SerializeField] private TileBase groundTile;

    // 世界位置 -> 单元格,然后放置瓦片
    public void PaintAt(Vector3 worldPos)
    {
        Vector3Int cell = tilemap.WorldToCell(worldPos);
        tilemap.SetTile(cell, groundTile);
    }

    public bool IsSolid(Vector3 worldPos)
        => tilemap.GetTile(tilemap.WorldToCell(worldPos)) != null;
}

2. Bulk fill a region (faster than per-cell
SetTile
)

2. 批量填充区域(比逐个单元格调用
SetTile
更快)

csharp
// SetTilesBlock writes a whole BoundsInt in one call — use it for procedural rooms/floors.
public void FillFloor(Tilemap map, TileBase tile, int width, int height)
{
    var bounds = new BoundsInt(0, 0, 0, width, height, 1);
    var tiles  = new TileBase[width * height];
    for (int i = 0; i < tiles.Length; i++) tiles[i] = tile;
    map.SetTilesBlock(bounds, tiles);
}
csharp
// SetTilesBlock一次写入整个BoundsInt区域 — 适用于程序化生成房间/地板
public void FillFloor(Tilemap map, TileBase tile, int width, int height)
{
    var bounds = new BoundsInt(0, 0, 0, width, height, 1);
    var tiles  = new TileBase[width * height];
    for (int i = 0; i < tiles.Length; i++) tiles[i] = tile;
    map.SetTilesBlock(bounds, tiles);
}

3. Clearing and refreshing

3. 清除与刷新

csharp
tilemap.SetTile(cell, null);   // null erases the tile at that cell
tilemap.RefreshTile(cell);     // re-evaluate just this cell's rule/animated neighbors (cheap)
// RefreshAllTiles() re-evaluates the ENTIRE map — reserve it for full regenerations, not per-edit.
csharp
tilemap.SetTile(cell, null);   // 传入null会清除该单元格的瓦片
tilemap.RefreshTile(cell);     // 仅重新计算该单元格的规则/动画邻居(性能开销低)
// RefreshAllTiles()会重新计算整个地图 — 仅在完全重新生成地图时使用,不要在每次编辑后调用。

Pitfalls

常见陷阱

  • RuleTile
    type not found
    — it's not built in. Install the 2D Tilemap Extras package (
    com.unity.2d.tilemap.extras
    ) via Package Manager.
  • A collider per tile tanks performance / leaves seams — add a
    CompositeCollider2D
    with a static
    Rigidbody2D
    and Used By Composite to merge the whole layer into one smooth shape.
  • Confusing world and cell coordinates
    SetTile
    /
    GetTile
    take a
    Vector3Int
    cell, not a world position. Always convert with
    WorldToCell
    /
    CellToWorld
    .
  • Tiles render behind/in front of sprites unexpectedly — set each
    TilemapRenderer
    's Sorting Layer and Order in Layer; multiple tilemaps need explicit ordering.
  • Rule/animated tiles don't update after script edits — refresh after writes, but prefer the targeted
    RefreshTile(cell)
    for a few edits;
    RefreshAllTiles()
    re-evaluates every tile on the map and stalls large levels. Reserve the full refresh for whole-map regenerations.
  • Painting onto the wrong layer — the active target Tilemap in the Tile Palette determines where paint lands; check the "Active Tilemap" dropdown.
  • 找不到
    RuleTile
    类型
    — 该类型并非内置。需通过Package Manager安装2D Tilemap Extras包(
    com.unity.2d.tilemap.extras
    )。
  • 每个瓦片一个碰撞器会导致性能下降/存在间隙 — 添加带静态
    Rigidbody2D
    CompositeCollider2D
    ,并启用_Used By Composite_,将整个图层合并为一个平滑的碰撞形状。
  • 混淆世界坐标与单元格坐标
    SetTile
    /
    GetTile
    接收的是
    Vector3Int
    类型的_单元格_坐标,而非世界位置。务必使用
    WorldToCell
    /
    CellToWorld
    进行转换。
  • 瓦片渲染在精灵前后位置异常 — 设置每个
    TilemapRenderer
    的Sorting Layer和Order in Layer;多个tilemap需明确设置排序顺序。
  • 脚本编辑后规则/动画瓦片未更新 — 写入后需进行刷新,少量编辑时优先使用针对性的
    RefreshTile(cell)
    RefreshAllTiles()
    会重新计算地图上的所有瓦片,在大型关卡中会造成卡顿。仅在完全重新生成地图时使用全量刷新。
  • 绘制到错误图层 — Tile Palette中激活的目标Tilemap决定了绘制位置;请检查“Active Tilemap”下拉菜单。

References

参考资料

  • Primary docs: Unity Manual "Tilemaps" (
    https://docs.unity3d.com/Manual/tilemaps/work-with-tilemaps/tilemap-reference.html
    ),
    ScriptReference/Tilemaps.Tilemap
    , and the 2D Tilemap Extras package manual (
    https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@1.6/manual/index.html
    ) for Rule Tiles.
  • 主要文档:Unity手册《Tilemaps》(
    https://docs.unity3d.com/Manual/tilemaps/work-with-tilemaps/tilemap-reference.html
    )、
    ScriptReference/Tilemaps.Tilemap
    ,以及Rule Tiles相关的2D Tilemap Extras包手册(
    https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@1.6/manual/index.html
    )。

Related skills

相关技能

  • level-design
    — engine-agnostic blockout, pacing, and tile layout practice.
  • procedural-gen
    — generating the tile data (noise, RNG, dungeons) you feed to
    SetTilesBlock
    .
  • platformer
    /
    roguelike
    — genres that compose this skill with movement and generation.
  • level-design
    — 与引擎无关的区块搭建、节奏规划和瓦片布局实践。
  • procedural-gen
    — 生成用于
    SetTilesBlock
    的瓦片数据(噪声、随机数生成、地牢等)。
  • platformer
    /
    roguelike
    — 将该技能与移动、生成机制结合的游戏类型。