2013-02-27 2 views
1

Update-function에서 재생되는 애니메이션이 있습니다 (Switch 경우).애니메이션이 유니티 3d로 완료 될 때까지 기다리십시오.

애니메이션이 끝나면 부울이 true로 설정됩니다.

내 코드 : goboolstartbool가 애니메이션을 종료하지 않고 바로 설정 얻을 내

case "play":  
    animation.Play("play");  
    gobool = true; 
    startbool = false; 
    break; 

문제입니다. 애니메이션이 끝날 때까지 어떻게 프로그램을 기다릴 수 있습니까?

답변

6

은 기본적으로 당신이 작동이 솔루션에 대한 두 가지 작업을 수행해야합니다

    애니메이션을 시작
  1. .
  2. 다음 애니메이션을 재생하기 전에 애니메이션이 끝날 때까지 기다립니다.

이 작업을 수행 할 수있는 방법의 예는 다음과 같습니다

animation.PlayQueued("Something"); 
yield WaitForAnimation(animation); 

그리고 WaitForAnimation에 대한 정의는 다음과 같습니다

C 번호 :

private IEnumerator WaitForAnimation (Animation animation) 
{ 
    do 
    { 
     yield return null; 
    } while (animation.isPlaying); 
} 

JS :

function WaitForAnimation (Animation animation) 
{ 
    yield; while (animation.isPlaying) yield; 
} 

do-while 루프는 동일한 프레임 PlayQueued에서 animation.isPlayingfalse을 반환한다는 것을 보여주는 실험에서 나왔습니다.

IEnumerator Start() 
{ 
    yield return animation.WhilePlaying("Something"); 
} 

Source, alternatives and discussion.

:

public static class AnimationExtensions 
{ 
    public static IEnumerator WhilePlaying(this Animation animation) 
    { 
     do 
     { 
      yield return null; 
     } while (animation.isPlaying); 
    } 

    public static IEnumerator WhilePlaying(this Animation animation, 
    string animationName) 
    { 
     animation.PlayQueued(animationName); 
     yield return animation.WhilePlaying(); 
    } 
} 

마지막으로 쉽게 코드에서 이것을 사용할 수 있습니다 :이 같은 단순화 당신이 애니메이션에 대한 확장 메서드를 만들 수 있습니다 약간의 땜질로

관련 문제