A coroutine allows you to spread tasks across several frames. In Unity, a coroutine is a method that can pause execution and return control to Unity but then continue where it left off on the following frame.
출처
코루틴은 여러 프레임에 테스트를 나눠줄 수 있게 도와준다. 유니티에서의 코루틴은 실행을 멈추고 제어를 유니티에게 넘기는 방식으로, 중단된 이후 시점으로 돌아가 계속 처리를 할 수 있다.
싱글 스레드로 비동기 방식을 구현한다. 병렬처리가 아닌 순차처리로 분할처리를 할 수 있게 한다.
따라서 비동기방식(ex. 멀티스레딩)이 아닌 비동기처럼 보이는 동기 시스템.
코루틴 함수가 IEnumerator를 반환하는것이 큰 핵심. ( 작업을 분할하여 수행하는 역할 )
그리고 일반 함수의 Return 대신 yield return을 사용해서 함수가 끝난 위치를 기억 및 다음에 다시 동작을 시작할때 어디서부터 시작해야할지 정보를 가지고 있다.
따라서 여러개 코루틴 함수를 실행시키고 yield로 어떤 시점에 반환, 그리고 어떤 시점에서부터 다시 시작해야하는지 정보를 가지고 있게 해서 멀티쓰레딩과 같은 효과를 가질 수 있게 해준다.
new WaitForEndOfFrame() - stops execution until the end of the next frame
//다음 프레임까지 실행을 멈춘다.
new WaitForFixedUpdate() - stops execution until the next physics engine frame.
//다음 물리엔진프레임까지 실행을 멈춘다.
new WaitForSeconds(float x) - stops execution for x seconds of game time (can be changed via Time.timeScale )
//게임시간으로 x초만큼 실행을 멈춘다.
new WaitForSecondsRealtime(float x) - stops execution for x seconds of real time
//실제 시간으로 x초만큼 실행을 멈춘다.
new WaitUntil(Func<bool>) - suspends the coroutine execution until the supplied delegate evaluates to true.
//Func<bool>아 true가 될때까지 실행중단.
new WaitWhile(Func<bool>) - inverse of WaitUntil, continues execution when Func returns false
//Func<boo>이 false일때 실행중단
null - Same as WaitForEndOfFrame(), but execution continues at the beginning of the next. frame
//다음프레임까지 멈춘다. 실행은 다음 프레임에 시작된다.
break - ends the coroutine
//코루틴의 끝.
StartCoroutine() - execution stops until the moment when the newly started coroutine ends.
//새로 시작한 코루틴이 끝날 때까지 실행을 멈춘다.