Son*_*ona 1 c# animation camera boolean unity-game-engine
Update
在一个Switch
案例中,我有一个动画在-function 中播放.
动画结束后,将布尔值设置为true.
我的代码:
case "play":
animation.Play("play");
gobool = true;
startbool = false;
break;
Run Code Online (Sandbox Code Playgroud)
问题是我的,gobool
并startbool
立即设置而不完成动画.我怎样才能让我的程序等到动画结束?
基本上,您需要为此解决方案做两件事:
如何做到这一点的一个例子是:
animation.PlayQueued("Something");
yield WaitForAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
而定义WaitForAnimation
将是:
C#:
private IEnumerator WaitForAnimation (Animation animation)
{
do
{
yield return null;
} while (animation.isPlaying);
}
Run Code Online (Sandbox Code Playgroud)
JS:
function WaitForAnimation (Animation animation)
{
yield; while ( animation.isPlaying ) yield;
}
Run Code Online (Sandbox Code Playgroud)
do-while循环来自实验,表明调用同一帧PlayQueued 中的animation.isPlaying
返回false
值.
通过一些修补,您可以为动画创建一个扩展方法,简化了这种方法,例如:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,您可以在代码中轻松使用它:
IEnumerator Start()
{
yield return animation.WhilePlaying("Something");
}
Run Code Online (Sandbox Code Playgroud)