게임개발 캠프 28일차

IIRU·2026년 6월 24일

Coroutine

지연동작중에 하나로써, 함수를 실행할 때 중간에 잠시 멈추거나, 시간을 기다려야 하는 작업을 할 때 사용할 수 있는 루틴이다.

선언

 IEnumerator CoolTime(float time)
 {
     canAttack = false;
     yield return new WaitForSeconds(time);
     canAttack = true;
     Debug.Log("공격가능");
 }

IEnumerator로 사용하며, yield return 값이 있어야한다.

Coroutine 시작과 정지

시작

StartCoroutine(//Coroutine());

정지

StopCoroutine();

정지와 같은 경우는 메모리에 만들어둔 함수를 넣게 되면 메모리마다 다른 공간에 생성되기 때문에 전에 action을 하듯이

Coroutine coroutine;
coroutine = CoolTime();
StopCoroutine(coroutine);

위 코드처럼 해준 후에 사용해 주어야 한다.

StopAllCoroutine();

🚫 StopAllCoroutine()은 실행한 MonoBehaviour 전체에서 실행된다.

Coroutine 함수들(WaitForSecond, WaitWhile, …)

yield return new WaitForSeconds(3f); //지정한 초만큼 대기.
yield return null; //한프레임 대기.
yield return new WaitForEndOfFrame(); //한 프레임의 렌더링이 끝날 때까지 대기.
yield return new WaitForFixedUpdate(); //FixedUpdate가 끝날 때까지 대기.
 yield return new WaitUntil(() => t > 10f); //조건이 true가 될때까지
yield return new WaitWhile(() => t < 10f); //조건이 true일 동안 대기
yield return StartCoroutine(DebugCoroutine()); //이 코루틴이 끝날때까지 대기
yield return WaitForFixedUpdate //물리 관련 동작할 때 사용
profile
초보 개발자 블로그입니다!

0개의 댓글