Wait for an animation to finish in unity3d(等待动画在unity3d中完成)
问题描述
我有一个在 Update 函数中播放的动画,在 Switch 的情况下.
I have an animation which plays in the Update-function, in a Switch case.
动画结束后,布尔值被设置为真.
After the animation finishes, a Boolean is being set to true.
我的代码:
case "play":
animation.Play("play");
gobool = true;
startbool = false;
break;
问题是我的 gobool 和 startbool 在没有完成动画的情况下立即设置.如何让我的程序等到动画完成?
The problem is my gobool and startbool get set immediately without finishing the animation. How can I let my program wait untill the animation is finished?
推荐答案
基本上你需要做两件事才能让这个解决方案发挥作用:
Basically you need to do two things for this solution to work:
- 开始动画.
- 等待动画完成,然后再播放下一个动画.
如何做到这一点的一个例子是:
An example of how this could be done is:
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 循环来自实验表明 animation.isPlaying 在调用 PlayQueued 的同一帧中返回 false.
The do-while loop came from experiments that showed that the animation.isPlaying returns false in the same frame PlayQueued is called.
稍加修改,您就可以为动画创建一个扩展方法来简化此操作,例如:
With a little tinkering you can create a extension method for animation that simplifies this such as:
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();
}
}
最后你可以轻松地在代码中使用它:
Finally you can easily use this in code:
IEnumerator Start()
{
yield return animation.WhilePlaying("Something");
}
来源、替代方案和讨论.
这篇关于等待动画在unity3d中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:等待动画在unity3d中完成
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
