2016년 7월 12일 화요일

Unity Coroutine

애니메이션이 특정 부분 완료되기까지를 기다리는 코루틴의 예
출처

yield.

null - Update구문의 수행이 완료될 때까지 대기한다.

WaitForEndOfFrame - 현재 프레임의 렌더링 작업이 끝날 때까지 대기한다.

WaitForFixedUpdate - FixedUpdate구문의 수행이 완료될 때까지 대기한다.

WaitForSeconds - 지정한 초만큼 대기한다.

WWW - 웹에서 데이터의 전송이 완료될 때까지 대기한다.
(WaitForSeconds or null처럼 재시작한다.)

Another coroutine - 새로운 코루틴은 yielder가 재시작되기 전에 완료 될 것이다.




//Wait for an animation to be a certain amount complete
IEnumerator WaitForAnimation(string name, float ratio, bool play)
{
    //Get the animation state for the named animation
    var anim = animation[name];
    //Play the animation
    if(play) animation.Play(name);

    //Loop until the normalized time reports a value
    //greater than our ratio.  This method of waiting for
    //an animation accounts for the speed fluctuating as the
    //animation is played.
    while(anim.normalizedTime + float.Epsilon + Time.deltaTime < ratio)
        yield return new WaitForEndOfFrame();

}


애니메이션을 기다리는 코루틴을 다음과 같이 작성

IEnumerator Die()
{
       //Wait for the die animation to be 50% complete
       yield return StartCoroutine(WaitForAnimation("die",0.5f, true));
       //Drop the enemies on dying pickup
       DropPickupItem();
       //Wait for the animation to complete
       yield return StartCoroutine(WaitForAnimation("die",1f, false));
       Destroy(gameObject);

}

댓글 없음:

댓글 쓰기