我可能会问一些非常明显的事情,而且我忽略了一些事情,但是在我做某事之前我试图创造一个停顿。
我已经看到这在网上很多地方使用过 -
yield WaitForSeconds(2);
但是我收到了语法错误
"错误CS1528:预期;或=(不能指定构造函数参数 声明)(CS1528)(Assembly-CSharp)
让我感到困惑的是,我不确定yield
作为关键字的真正意义或作用,并且我假设WaitForSeconds
是" 2&#的开启类34;在建设者(不在声明中)任何帮助将不胜感激。谢谢!
答案 0 :(得分:5)
你想要的是使用IEnumerator。
IEnumerator Example()
{
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
然后你会问:我怎么称呼它?
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);
}
我刚刚阅读了link Jon Skeet发表的评论,我也推荐它,它有非常有价值的信息。
答案 1 :(得分:0)
您正在使用Unity的Javascript代码,并使用C#语言进行尝试,这就是您收到错误消息的原因。
如果您点击http://docs.unity3d.com/ScriptReference/WaitForSeconds.html等网页上的C#语言选择器 您将获得C#的以下示例代码:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
IEnumerator Example() {
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
}