경일게임아카데미 멀티 디바이스 메타버스 플랫폼 개발자 양성과정 20221006 개인 공부 2022/04/04~2022/12/14

Jinho Lee·2022년 10월 6일
0

2022.10.06 개인 학습 - 유니티 코루틴 최적화

유니티 코루틴 최적화

YieldInstruction

  • 코루틴 내부에서 yield 구문에 사용되는 값

    1. WaitForEndOfFrame

    2. WaitForFixedUpdate

    3. 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;
      }
    

0개의 댓글