코루틴 함수 형식
대기하는 함수다.
괜히 return 있다고 중단되는 게 아닌 대기하는 거다.
IEnumerator 함수()
{
yield return null;
//ㄴ 한 프레임만 기다린다.
//yield return new WaitForSeconds(2f);
//ㄴ 2초를 기다린다. [타임스케일 적용]
//yield return new WaitForSecondsRealtime(2f);
//ㄴ 2초를 기다린다. [타임스케일 무시]
//yield return new WaitForFixedUpdate();
//FixedUpdate가 끝날때까지
//yield return new WaitForEndofFrame();
//프레임[랜더링 포함]이 끝날 때 까지
//yield break;
//코루틴을 멈춘다
}
StartCoroutine(코루틴_함수()); //시작함수
StartCoroutine("코루틴_함수", 매개변수); //시작함수
//매개변수 => object => int. [박싱 => 언박싱] : 오버헤드가 생김
//매개변수가 다른 코루틴 함수는 오류처리가 안생김
StopCoroutine(코루틴_함수()); // 멈추는함수
StopAllCoroutines(); // 모든 코루틴을 멈추는함수
코루틴은 비활성화나 Awake에 생성하면 안된다.
오류가 생길 수 있기 때문
또한 안에 while문을 그냥 넣으면 반복만 하다가 끝날 수도 있다.
IEnumerator iEnumerator; //IEnumerator 생성
void Start()
{
iEnumerator = TestCoroutine();//IEnumerator에 함수 넣음
StartCoroutine(iEnumerator);
//여담으로 매개변수 문자열로도 가능함, 대신 연속으로 같은 문자열을 두번 실행할 경우 코루틴이 멈춤
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
StopCoroutine(iEnumerator);
}
IEnumerator TestCoroutine()
{
int i = 0;
while(true)
{
yield return null;
Debug.Log(i);
i++;
}
}