Unity3d

코루틴

scarecrow1992 2022. 2. 20. 23:33

 

https://docs.unity3d.com/kr/530/Manual/Coroutines.html

특정 효과를 점진적으로 주고 싶을경우 주로 사용한다.

주로 점차적인 애니메이션 혹은, 분산 연산(로딩 화면 etc)등에 사용됩니다.

 

1
StartCoroutine(DeathAnimation());
cs

 

1
2
3
4
5
6
7
8
9
public IEnumerator DeathAnimation() {
    int current = 0;
 
    for (; current > -90; current = current - 5) {
        transform.Rotate(new Vector3(-500));
        yield return null;
    }
    Die();
}
cs

yield return null; 에서 DeathAnimation이 반환되면, 다음번에 DeathAnimation이 호출될경우 yield return null;이 호출되었던 곳 부터 시작됩니다.

그동한 함수 내의 모든 변수값들은 자신의 상태를 저장하게 됩니다.

 

 

yield return new WaitForSeconds(float); 로 반환하면, 코루틴 호출 간격을 조절 할 수 있다.

1
2
3
4
5
IEnumerator UpdatePath() {
    // ...
 
    yield return new WaitForSeconds(refreshRate);
}
cs