2022.10.06 개인 학습 - 유니티 코루틴 최적화
코루틴 내부에서 yield 구문에 사용되는 값
WaitForEndOfFrame
WaitForFixedUpdate
WaitForSeconds
여태 하던 방식(이하 예시 코드 참고)으로 코루틴을 사용하면
매번 새로운 WaitForSeconds를 생성하고 파괴하여 Garbage가 발생한다.
private IEnumerator OnChangeColor()
{
_renderer.material.color = Color.red;
yield return new WaitForSeconds(0.1f);
_renderer.material.color = Color.white;
}
Garbage 생성을 막기 위해 YieldInstruction 변수를 선언, WaitForSeconds를 할당하는 것으로
캐싱(Caching)하여 고정된 WaitForSeconds를 반복적으로 사용할 수 있다.
private static readonly YieldInstruction DELAY_CHANGE_COLOR = new WaitForSeconds(0.1f);
private IEnumerator OnChangeColor()
{
_renderer.material.color = Color.red;
yield return DELAY_CHANGE_COLOR;
_renderer.material.color = Color.white;
}