방법1. 하나의 반복문에서 배치
방법2. 중첩 반복문으로 배치
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject cube;
void Start()
{
int row = 6;
int col = 2;
int dep = 12;
/* loop 1 */
// int cnt = row * col * dep;
// for (int i = 0; i < cnt; i++)
for (int i = 0; i < row * col * dep; i++)
{
GameObject go = Instantiate(cube);
float x = (i / col) % row;
float y = i % col;
float z = i / (col * row);
go.transform.position = new Vector3(x, y, z);
}
/* loop 2 */
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
for (int k = 0; k < dep; k++)
{
GameObject go = Instantiate(cube);
go.transform.position = new Vector3(i, j, k);
}
}
}
}
}
1번 방법. O(n), 수학적 로직에 익숙한 사람들 선호
2번 방법. O(n^3), 직관적 이해 쉬움

* 2D 퍼즐 게임에서 종종 사용되며, 3D에서도 사용됨
'내일배움캠프 > TIL' 카테고리의 다른 글
| [내배캠][Unity6기][TIL] Unity 입문 주차 팀프로젝트 발표 및 피드백 (0) | 2024.10.22 |
|---|---|
| [내배캠][Unity6기][TIL] 24.10.14 특강 - 메모리 (0) | 2024.10.15 |
| [내배캠][Unity6기][TIL] 키 입력 처리 함수 비교(GetKey, GetAxis, GetAxisRaw) (0) | 2024.10.08 |
| [내배캠][Unity6기][TIL] Unity 입문 (1~5) (0) | 2024.10.07 |
| [내배캠][Unity6기][TIL] C# 문법 종합 강의 4주차(3~4) (0) | 2024.10.05 |