Grid Manager.cs 프리팹 생성 후 우클릭해서 사이즈에 맞게끔 생성
void InitGrid(int size)
{
gridObj = Instantiate(new GameObject(), transform);
var layoutGroup = gridObj.AddComponent<GridLayoutGroup>();
gridObj.name = $" Grid {size}x{size} ";
var rect = gridObj.transform as RectTransform;
rect.localPosition = mPos;
rect.sizeDelta = mSlotSize;
layoutGroup.padding.left = mLeftPadding;
layoutGroup.spacing = new Vector2(15f, 15f);
layoutGroup.cellSize = new Vector2(mCellSize,mCellSize);
Vector2 pipePos = new Vector2(5.32f, 2.675f);
int idx = 1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
// 배경 슬롯 설정
var tmpObj = Instantiate(backObj, gridObj.transform);
tmpObj.name = $" Background Grid {idx} ";
// 메인 슬롯 설정
var childObj = tmpObj.transform.GetChild(0);
childObj.name = $" Main Grid {idx} ";
// 메인 슬롯 위치 설정
var childRect = childObj.transform as RectTransform;
childRect.localPosition = mMainGridPos;
childRect.sizeDelta = new Vector2(mCellSize, mCellSize);
// 선 위치 설정
var slot = childObj.GetComponent<Slot>();
slot.idx = idx - 1;
slot.pipePos = pipePos;
pipePos.x += 0.86f;
listSlot.Add(slot);
idx++;
}
pipePos.y -= 0.86f;
pipePos.x = 5.32f;
}
}
[ContextMenu("5x5 생성")]
public void Create5x5()
{
mPos = new Vector2(237f, 100f);
mSlotSize = new Vector2(1080f, 975f);
mMainGridPos = new Vector2(92.5f, -92.5f);
mLeftPadding = 50;
mCellSize = 185f;
InitGrid(5);
}
[ContextMenu("6x6 생성")]
public void Create6x6()
{
mPos = new Vector2(237f, 100f);
mSlotSize = new Vector2(1080f, 975f);
mMainGridPos = new Vector2(75f, -75f);
mLeftPadding = 50;
mCellSize = 150f;
InitGrid(6);
}
[ContextMenu("7x7 생성")]
public void Create7x7()
{
mPos = new Vector2(237f, 100f);
mSlotSize = new Vector2(1080f, 1000f);
mMainGridPos = new Vector2(65f, -65f);
mLeftPadding = 40;
mCellSize = 130f;
InitGrid(7);
}
[ContextMenu("8x8 생성")]
public void Create8x8()
{
mPos = new Vector2(237f, 87f);
mSlotSize = new Vector2(1080f, 1000f);
mMainGridPos = new Vector2(57.5f, -57.5f);
mLeftPadding = 30;
mCellSize = 115f;
InitGrid(8);
}
[ContextMenu("9x9 생성")]
public void Create9x9()
{
mPos = new Vector2(237f, 76f);
mSlotSize = new Vector2(1080f, 1020f);
mMainGridPos = new Vector2(50f, -50f);
mLeftPadding = 30;
mCellSize = 100f;
InitGrid(9);
}
[ContextMenu("삭제")]
public void DeleteGrid()
{
#if UNITY_EDITOR
// 삭제 후 클리어
DestroyImmediate(gridObj);
listSlot.Clear();
#endif
}
동그라미들을 설정해주자, 아래 char형 2차원 배열이 실질적인 레벨인데 보안을 위해 데이터베이스에 연동 시키는것도 하나의 방법
char[,] mLvl1 = new char[,]
{
{ '\0', '\0', '\0','\0','\0','6' },
{ '\0', '\0', '\0','\0','\0','\0' },
{ '\0', '3', '0','\0','\0','\0' },
{ '\0', '\0', '\0','2','\0','3' },
{ '\0', '2', '\0','6','0','1' },
{ '\0', '\0', '\0','1','\0','\0' }
};
GridManager.instance.SetLevel(mLvl1);
public void SetLevel(char[,] map)
{
if (listSlot.Count == 0)
return;
int pipeIdx = 0, totalPipes =0;
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
var letter = map[i, j];
var slot = listSlot[i + j + pipeIdx];
if (letter == '\0')
{
emptyCount++;
RectTransform rect = slot.GetComponent<RectTransform>();
float size = 100.0f;
rect.sizeDelta = new Vector2(size, size);
continue;
}
var color = Global.arrColor[letter - '0'];
GameManager.instance.dicCompleted.TryAdd(color, false);
dicEntryPointSlot.TryAdd(slot.pipePos, slot);
slot.img.sprite = knobSprite;
slot.img.color = color;
slot.color = slot.img.color;
totalPipes++;
}
pipeIdx += 5;
}
UI_Manager.instance.gameUI.UpdateTotalPipe(totalPipes / 2);
}
원형 색상은 아래와 같음 Global.cs
// ------- 색상 관련 -------
public static readonly Color red = Color.red;
public static readonly Color blue = Color.blue;
public static readonly Color green = Color.green;
public static readonly Color yellow = Color.yellow;
public static readonly Color purple = Color.magenta;
public static readonly Color cyan = Color.cyan;
public static readonly Color orange = new Color(1f, 0.6f, 0f);
public static readonly Color pink = new Color(255, 192, 203);
public static readonly Color brown = new Color(165, 42, 42);
public static Color[] arrColor = new Color[]
{
red,
blue,
green,
yellow,
purple,
cyan,
orange,
pink,
brown
};
결과
'게임엔진 > 개인 플젝' 카테고리의 다른 글
[Outdated] 농장 시뮬레이터 개인작 (0) | 2024.04.30 |
---|---|
플레이어 방향에 따른 발사 위치 변경 (0) | 2024.04.23 |
Flow Free 3 - UI (0) | 2024.03.27 |
Flow Free 2 - 슬롯(그리드)과 파이프(연결선) (0) | 2024.03.27 |
[Unity] Flow Free 게임 카피캣 만들기 (0) | 2023.12.05 |