본문 바로가기
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();

 

'Unity > 기초' 카테고리의 다른 글

Localization  (0) 2024.03.03
Layer, SoftLayer, Tag  (0) 2024.02.08
List, Dictionary  (0) 2024.02.08
클릭, 터치 상태에 따른 이벤트  (0) 2024.02.08
Move, Rotation, Quaternion  (0) 2024.02.08