Unity Coroutine override 유니티 코루틴 오버라이드

 

유니티에서 함수 오버라이드 같이 코루틴을 오버라이드 하고 싶은 경우가 있다.

스택오버플로우 같은데서 구글링 후 3-4명 정도의 답변이

yield return StartCoroutine(base.MyCoroutine()); 을 사용하라고 달려있다.

 

protected virtual IEnumerator MyCoroutine()
{
    yield return new WaitForSeconds(1f);
    Debug.Log("Base");
}
protected override IEnumerator MyCoroutine()
{
    Debug.Log("Override A");
    
    yield return new WaitForSeconds(1f);
    yield return StartCoroutine(base.MyCoroutine());
    
    Debug.Log("Override B");
}

 

하지만 실제로 적용해보면 예상대로 작동 되지 않는다.

 

Override A -> Base -> Override B 순으로 출력되길 원하지만

Override A -> Base 까지만 출력됨.

 

protected override IEnumerator MyCoroutine()
{
    Debug.Log("Override A");
    
    yield return new WaitForSeconds(1f);
    
    var baseLerp = base.MyCoroutine();
    yield return baseLerp.MoveNext();
    
    Debug.Log("Override B");
}

 

위와 같이 호출해줘야 정상적으로 작동하는 것을 확인할 수 있다. 

 

스택 오버플로우
https://answers.unity.com/questions/927356/overriding-coroutines-not-working-as-expected.html

 

 

나중에 다시 확인해봐야함!

 

댓글

Designed by JB FACTORY