2025/05/23(금)

개발자 지망생 유령·2025년 5월 23일

내일배움캠프

목록 보기
17/17

코루틴
Unity에서 일반적인 함수는 한 번에 실행되지만
코루틴은 일시 중지하고 다시 재개할 수 있는 함수

기본 사용법

코루틴선언

IEnumerator MyCoroutine()
{
    Debug.Log("Start");
    yield return new WaitForSeconds(2f);//2초대기
    Debug.Log("2초 후 출력");
}

코루틴 시작

Coroutine myRoutine;

void StartCoroutine()
{
    myRoutine = StartCoroutine(MyCoroutine());//MyCoroutine()실행 
}
void StopCoroutine()
{
// myRoutine변수에 저장된 실행중인 코루틴 인스턴스 정지
    StopCoroutine(MyCoroutine); 
}

yield return 타입
ex) yield return null;

null 다음 프레임까지 대기
new WaitForSeconds(seconds) 지정한 초만큼 대기
new WaitUntil(() => 조건) 조건이 참이 될 때까지 대기
new WaitWhile(() => 조건) 조건이 거짓이 될 때까지 대기

사용처

대시 타이밍 조절
공격 쿨타임
비주얼 이펙트 딜레이
특정 조건을 기다릴 때
버프

0개의 댓글