Chuyển đến nội dung
Skillpedia
Lập trình chung6.6k · #13851

unity-tilemap-2d

Skill này làm gì

Skill này giải thích cách xây và viết script cho bản đồ tile 2D trong Unity 6. Nó dành cho nhà phát triển tạo màn chơi 2D từ ảnh tile.

Khi nào nên dùng

  • ✓Khi bạn muốn vẽ màn chơi 2D bằng Tile Palette trong Unity.
  • ✓Khi tile cần va chạm, hoặc các cạnh cần tự nối bằng Rule Tile.
  • ✓Khi bạn cần script tạo hoặc sửa tile trong lúc game đang chạy.

Liên kết skill

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-tilemap-2d/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-tilemap-2d/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Giúp tôi vẽ một màn chơi platformer 2D trong Unity và làm các bức tường rắn chắc với người chơi.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill giải thích cách thiết lập lưới và tilemap, thêm va chạm cho bản đồ, dùng rule tile để tự tạo cạnh, và thay đổi tile từ code.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Unity 2D Tilemap

Author and script tile-based 2D levels in Unity 6.3 LTS with the Grid/Tilemap system, the Tile Palette, colliders, and runtime painting. Targets Unity 6.3 LTS (6000.3).

Package note: the core Tilemap (Grid, Tilemap, Tile, TilemapCollider2D) is built in, and so is the Tile Palette with its default brush. Rule Tiles, Animated Tiles, and the extra brushes (Random, Line, GameObject, Group) live in the separate "2D Tilemap Extras" package (com.unity.2d.tilemap.extras, 6.0.x for Unity 6.3) — install it via Package Manager before using RuleTile.

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.

Core workflow

  1. Create the grid: GameObject → 2D Object → Tilemap → Rectangular (the Grid's Cell Layout also offers Hexagonal and Isometric for hex/iso levels). 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 set the tilemap collider's Composite Operation to Merge.
  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.

Patterns

1. Painting tiles at runtime (cell coordinates)

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;
}

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

// 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);
}

3. Clearing and refreshing

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.

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 set Composite Operation to Merge to merge the whole layer into one smooth shape.
  • No "Used By Composite" checkbox, or an obsolete warning on usedByComposite — Unity 6 replaced it with Composite Operation (Collider2D.compositeOperation). Old usedByComposite = true maps to CompositeOperation.Merge; write that instead.
  • Small gaps remain between merged tiles — raise the Tilemap Collider 2D's Extrusion Factor so
Câu hỏi thường gặp

Tôi có cần gói thêm cho Rule Tile không?

Có. Rule Tile đến từ gói 2D Tilemap Extras, và phải cài gói này trước.

Skill này có thiết kế bố cục màn chơi không?

Không. Nguyên tắc thiết kế màn chơi do một skill thiết kế màn chơi riêng bao quát.

Skill này có bao quát lưới 3D không?

Không. Đặt tile 3D nằm ngoài skill này.

Chi tiết

Mô tả gốc của tác giả

Build and script 2D tilemaps in Unity 6.3 LTS: the Grid + Tilemap components, the Tile Palette, tilemap colliders, rule tiles, and runtime SetTile/GetTile painting. Use when painting tile levels, adding a TilemapCollider2D, using rule or animated tiles, generating tilemaps from code, or when the user mentions Unity tilemap, tile palette, rule tile, or Grid.

repo: gamedev-skills/awesome-gamedev-agent-skills

Độ phổ biến

Sao GitHub
6.6k
Vị trí xếp hạng
#13851
Giấy phép
Apache-2.0
Cập nhật
28/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự