unity-tilemap-2d
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity 2D Tilemap
Unity 2D 瓦片地图
Author and script tile-based 2D levels in Unity 6 with the / system, the Tile
Palette, colliders, and runtime painting. Targets Unity 6 (6000.0 LTS).
GridTilemapPackage note: the core Tilemap (,Grid,Tilemap,Tile) is built in. Rule Tiles, Animated Tiles, and Tile Palette brushes live in the separate "2D Tilemap Extras" package (TilemapCollider2D) — install it via Package Manager before usingcom.unity.2d.tilemap.extras.RuleTile
借助/系统、Tile Palette、碰撞器和运行时绘制功能,在Unity 6中创作并编写基于瓦片的2D关卡脚本。适配**Unity 6 (6000.0 LTS)**版本。
GridTilemap**包说明:**核心Tilemap功能(、Grid、Tilemap、Tile)已内置。Rule Tiles、Animated Tiles和Tile Palette画笔位于独立的"2D Tilemap Extras"包(TilemapCollider2D)中——使用com.unity.2d.tilemap.extras前需通过Package Manager安装该包。RuleTile
When to use
适用场景
- Use when laying out a 2D level by painting tiles, setting up a +
Grid, adding collision to the map, using auto-tiling Rule Tiles, or generating/editing tiles from script.Tilemap - Use when scenes contain a with
Gridchildren, orTilemaptile/palette files.*.asset
When not to use: level design practice (pacing, blockout, layout principles) →
. 3D tile/grid placement → Unity's own 3D tooling (ProBuilder / grid brushes; no dedicated skill here). The platformer character that
moves over the tiles → / .
level-designplatformerunity-physics- 适用于通过绘制瓦片布局2D关卡、设置+
Grid、为地图添加碰撞、使用自动瓦片的Rule Tiles,或通过脚本生成/编辑瓦片的场景。Tilemap - 适用于场景中包含带有子对象的
Tilemap,或存在Grid瓦片/调色板文件的场景。*.asset
**不适用场景:**关卡设计实践(节奏规划、区块搭建、布局原则)→ 参考。3D瓦片/网格放置→使用Unity自带的3D工具(ProBuilder/网格画笔;此处无对应专属技能)。在瓦片上移动的平台角色→参考 / 。
level-designplatformerunity-physicsCore workflow
核心工作流程
- Create the grid: GameObject → 2D Object → Tilemap → Rectangular. This makes a with a child
Grid(+Tilemap). Use one Tilemap per layer (background, ground, foreground) and set each renderer's sorting.TilemapRenderer - Open the Tile Palette (Window → 2D → Tile Palette), drag in a sliced sprite sheet to
create assets, then paint with the brush tools.
Tile - Add collision to the solid layer: . For one merged collider (far cheaper and gap-free), also add
TilemapCollider2D(+ aCompositeCollider2Dset to Static) and enable Used By Composite on the tilemap collider.Rigidbody2D - Auto-tile with Rule Tiles (2D Tilemap Extras) so edges/corners pick the right sprite automatically instead of hand-placing every variant.
- Edit at runtime via script with cell coordinates ():
Vector3Int,SetTile,GetTile, converting world↔cell with theSetTilesBlock/Grid.Tilemap - Verify in Play mode: confirm collisions (Physics Debugger), sorting order, and that
ran after bulk edits.
RefreshAllTiles
- **创建网格:**依次点击GameObject → 2D Object → Tilemap → Rectangular。这会创建一个带有子对象(+
Tilemap)的TilemapRenderer。为每个图层(背景、地面、前景)使用一个Tilemap,并设置每个渲染器的排序层级。Grid - 打开Tile Palette(Window → 2D → Tile Palette),拖入切片后的精灵图集以创建资源,然后使用画笔工具进行绘制。
Tile - 为实体图层添加碰撞:添加。若要合并为单个碰撞器(性能更优且无间隙),还需添加
TilemapCollider2D(+设置为Static的CompositeCollider2D),并在瓦片地图碰撞器上启用_Used By Composite_选项。Rigidbody2D - 使用Rule Tiles(2D Tilemap Extras)实现自动瓦片,使边缘/角落自动选择正确的精灵,无需手动放置每个变体。
- 通过脚本在运行时编辑:使用单元格坐标()调用
Vector3Int、SetTile、GetTile方法,通过SetTilesBlock/Grid实现世界坐标与单元格坐标的转换。Tilemap - 在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
)
SetTile2. 批量填充区域(比逐个单元格调用SetTile
更快)
SetTilecsharp
// 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
常见陷阱
- type not found — it's not built in. Install the 2D Tilemap Extras package (
RuleTile) via Package Manager.com.unity.2d.tilemap.extras - A collider per tile tanks performance / leaves seams — add a with a static
CompositeCollider2Dand Used By Composite to merge the whole layer into one smooth shape.Rigidbody2D - Confusing world and cell coordinates — /
SetTiletake aGetTilecell, not a world position. Always convert withVector3Int/WorldToCell.CellToWorld - Tiles render behind/in front of sprites unexpectedly — set each 's Sorting Layer and Order in Layer; multiple tilemaps need explicit ordering.
TilemapRenderer - Rule/animated tiles don't update after script edits — refresh after writes, but prefer
the targeted for a few edits;
RefreshTile(cell)re-evaluates every tile on the map and stalls large levels. Reserve the full refresh for whole-map regenerations.RefreshAllTiles() - Painting onto the wrong layer — the active target Tilemap in the Tile Palette determines where paint lands; check the "Active Tilemap" dropdown.
- 找不到类型 — 该类型并非内置。需通过Package Manager安装2D Tilemap Extras包(
RuleTile)。com.unity.2d.tilemap.extras - 每个瓦片一个碰撞器会导致性能下降/存在间隙 — 添加带静态的
Rigidbody2D,并启用_Used By Composite_,将整个图层合并为一个平滑的碰撞形状。CompositeCollider2D - 混淆世界坐标与单元格坐标 — /
SetTile接收的是GetTile类型的_单元格_坐标,而非世界位置。务必使用Vector3Int/WorldToCell进行转换。CellToWorld - 瓦片渲染在精灵前后位置异常 — 设置每个的Sorting Layer和Order in Layer;多个tilemap需明确设置排序顺序。
TilemapRenderer - 脚本编辑后规则/动画瓦片未更新 — 写入后需进行刷新,少量编辑时优先使用针对性的;
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, and the 2D Tilemap Extras package manual (ScriptReference/Tilemaps.Tilemap) for Rule Tiles.https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@1.6/manual/index.html
- 主要文档:Unity手册《Tilemaps》()、
https://docs.unity3d.com/Manual/tilemaps/work-with-tilemaps/tilemap-reference.html,以及Rule Tiles相关的2D Tilemap Extras包手册(ScriptReference/Tilemaps.Tilemap)。https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@1.6/manual/index.html
Related skills
相关技能
- — engine-agnostic blockout, pacing, and tile layout practice.
level-design - — generating the tile data (noise, RNG, dungeons) you feed to
procedural-gen.SetTilesBlock - /
platformer— genres that compose this skill with movement and generation.roguelike
- — 与引擎无关的区块搭建、节奏规划和瓦片布局实践。
level-design - — 生成用于
procedural-gen的瓦片数据(噪声、随机数生成、地牢等)。SetTilesBlock - /
platformer— 将该技能与移动、生成机制结合的游戏类型。roguelike