Unity
디펜스 게임 만들기 - 그리드 타일 시스템
대용킹
2024. 2. 5. 01:32

그러니까 순서는
0. 게임이 시작된다.
1. 안쪽에 초록색 타일을 생성하여 채운다.
2. 생성할 때 타일의 위치와 기초데이터들을 초기화 한다.
구현
public class GridSystem : MonoBehaviour
{
public Tile tile;
public int xSize, ySize;
public float xSpace, ySpace;
[SerializedDictionary("TilePos", "Tile")]
public Dictionary<string, Tile> tileDic = new Dictionary<string, Tile>();
private void Start()
{
for (int i = 0; i < xSize; i++)
{
for (int j = 0; j < ySize; j++)
{
Tile newTile = Instantiate(tile, new Vector3(0.5f + i * xSpace, 0.5f, 0.5f + j * ySpace), Quaternion.identity);
TilePos tilePos = new TilePos(i, j);
string key = i + "," + j;
newTile.transform.parent = transform;
newTile.InitiateTile(tilePos, this);
tileDic.Add(key, newTile);
}
}
}
결과

다음은 타일의 데이터를 초기화 한다.
public class Tile : MonoBehaviour
{
public Material material;
public TilePos tilePos;
public GridSystem gridSystem;
public void InitiateTile(TilePos tilePos,GridSystem gridSystem)
{
material = GetComponent<MeshRenderer>().material;
this.tilePos = tilePos;
this.gridSystem = gridSystem;
gameObject.name = tilePos.x + " , " + tilePos.y;
}
#region DebugLogic
public void Debug_Select()
{
gridSystem.GetNearTile(tilePos);
}
public void TileColorChange(Color color)
{
material.DOColor(color, 0.5f).SetLoops(2, LoopType.Yoyo);
}
#endregion
}
[System.Serializable]
public struct TilePos
{
public int x, y;
public TilePos(int x, int y)
{
this.x = x;
this.y = y;
}
}
내일은 타일을 클릭하면 주변 타일을 검색하는 기능을 만들겠다.