使用yield WaitForSeconds [英] Using yield WaitForSeconds
问题描述
我可能正在问一个非常明显的问题,而我却忽略了一个问题,但是我试图在它做某事之前先创建一个暂停.
I might be asking something that's extremely obvious and I have overlooked something, but I'm trying to create a pause before it does something.
我已经看到它在网上很多地方都被使用-
I have seen this being used in many places online -
yield WaitForSeconds(2);
但是我得到的语法错误是
However I get a syntax error of,
错误CS1528:预期;或=(无法指定构造函数参数声明中)(CS1528)(Assembly-CSharp)
"Error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) (CS1528) (Assembly-CSharp)
这让我感到困惑,因为我不太确定关键字 yield
的真正含义或作用,而且在 WaitForSeconds
是其上的类的前提下在构造函数中(而不是在声明中)"2"表示任何帮助.谢谢!
Which confuses me as im not really sure what yield
as a keyword really means or does, and im under the assumption that WaitForSeconds
is a class of its on with the "2" being in the constructor (not in a declaration) any help would be appreciated. thanks!
推荐答案
您要使用的是IEnumerator.
What you want is t use an IEnumerator.
IEnumerator Example()
{
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
然后您会问:我怎么称呼它?
Then you will ask: How do I call this?
void Start()
{
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(2.0F));
print("Before WaitAndPrint Finishes " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime)
{
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
我刚刚阅读了评论上发布的链接 Jon Skeet,我也推荐它,它非常有价值信息.
I just read the link Jon Skeet posted on the comments, I too recommend it, it has pretty valuable information.
这篇关于使用yield WaitForSeconds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!