개발툴/Unity

Unity Tilemap을 이용해서 원하는 크기의 격자맵 그리기

가든_ 2023. 9. 30. 22:20

Unity Tilemap의 SetTile을 사용하면 타일 설정이 가능하다.

참고자료

https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetTile.html

 

Unity - Scripting API: Tilemaps.Tilemap.SetTile

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

활용 코드

using UnityEngine;
using UnityEngine.Tilemaps;

public class Board : MonoBehaviour
{
    public Tilemap tilemap { get; private set; }

    private void Awake()
    {
        tilemap = GetComponent<Tilemap>();
    }

    /// <summary>
    /// state의 행과 열의 크기만큼 tilemap을 설정한다.
    /// </summary>
    public void Draw(Cell[,] state)
    {
        int width = state.GetLength(0); // 행의 개수
        int height = state.GetLength(1); // 열의 개수

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Cell cell = state[x, y];
                tilemap.SetTile(cell.position, GetTile(cell));
            }
        }
    }

    private Tile GetTile(Cell cell)
    {
        //...원하는 타일을 리턴한다.
        return tile;
    }

}