Unity/기초
Coroutine
by 롤다
2024. 2. 8.
- WaitForFixedUpdate(): 다음 FixedUpdate 물리 프레임 전까지 일시 중지
- WaitForSecondsRealtime(5): 크기 조정되지 않은 시간을 사용하여 지정된 시간(초) 동안 코루틴 실행을 일시 중지
- WaitForSeconds(5): 조정된 시간을 사용하여 지정된 시간(초) 동안 코루틴 실행을 일시 중지
- WaitForEndOfFrame(): 모든 렌더링 작업이 끝날 때까지 일시 중지
- null: 다음 프레임까지 대기
- StartCoroutine(코루틴): 코루틴을 연결하고 코루틴이 완료될 때까지 일시 중지
// Coroutine 재사용 최적화
IEnumerator MyCoroutine;
// Wait 재사용 최적화
readonly WaitForFixedUpdate CoroutineRe = new();
public void MyCoroutineStart()
{
if (MyCoroutine != null)
{
StopCoroutine(MyCoroutine); // 코루틴이 이미 실행되고 있으면 중지
}
MyCoroutine = Cool(1); // 1초
StartCoroutine(MyCoroutine);
}
IEnumerator Cool(float cool)
{
while (cool >= 0f)
{
cool -= Time.deltaTime;
yield return CoroutineRe;
}
// 실행
}
// 이 스크립트 안에 코루틴은 모두 중단
StopAllCoroutines();