C
unity 루프 loop
unix
2023. 4. 26. 12:06
루프는 액션을 반복하는 수단 - for 루프, while 루프, DoWhile 루프
1. while
조건이 충족되면 반복합니다.
using UnityEngine;
using System.Collections;
public class WhileLoop : MonoBehaviour
{
int cupsInTheSink = 4;
void Start ()
{
while(cupsInTheSink > 0)
{
Debug.Log ("I've washed a cup!");
cupsInTheSink--;
}
}
}
2. DoWhile
반복을 한번 하고나서 조건을 충족하는지 검사합니다.
; 세미콜론이 존재하므로 조심합니다.
using UnityEngine;
using System.Collections;
public class DoWhileLoop : MonoBehaviour
{
void Start()
{
bool shouldContinue = false;
do
{
print ("Hello World");
}while(shouldContinue == true);
}
}
3. For
반복 횟수를 조정할 수있는 루프.
루프의 조건을 검사하며 시작.
using UnityEngine;
using System.Collections;
public class ForLoop : MonoBehaviour
{
int numEnemies = 3;
void Start ()
{
for(int i = 0; i < numEnemies; i++)
{
Debug.Log("Creating enemy number: " + i);
}
}
}
출처 - https://learn.unity.com/tutorial/rupeu-eap?uv=2019.3&projectId=625e4bf5edbc2a03c37c9827