본문 바로가기

C

unity 루프 loop

 

루프는 액션을 반복하는 수단 - 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

 

 

 

 

'C' 카테고리의 다른 글

unity Awake and Start  (0) 2023.04.27
unity 변수 및 함수의 범위와 접근성 Scope and Access modifiers  (0) 2023.04.27
unity if문  (0) 2023.04.26
unity 규칙 및 구문  (0) 2023.04.25
unity 변수 및 함수  (0) 2023.04.25