【发布时间】:2014-04-15 05:16:58
【问题描述】:
这一切都始于昨天我使用该代码制作 sn-p 时:
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);
}
发件人:http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
问题是我想从该类型的类中设置一个静态变量:
class example
{
private static int _var;
public static int Variable
{
get { return _var; }
set { _var = value; }
}
}
问题出在哪里?问题是,如果将该变量放在函数的参数中,则此“时间参数”将在函数结束时被销毁......所以,我搜索了一下,我记得:
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
发件人:http://msdn.microsoft.com/es-es/library/ms228503.aspx
但是,(总是有一个“但是”)Iterators cannot have ref or out...所以我决定创建自己的等待函数,因为 Unity 没有睡眠功能...
这是我的代码:
Debug.Log("Showing text with 1 second of delay.");
float time = Time.time;
while(true) {
if(t < 1) {
t += Time.deltaTime;
} else {
break;
}
}
Debug.Log("Text showed with "+(Time.time-time).ToString()+" seconds of delay.");
该代码有什么问题?问题是它是非常粗暴的代码,因为它会产生内存泄漏、错误,当然还有应用程序的播放...
那么,你建议我做什么?
【问题讨论】:
-
你很可能不想睡觉。这很少是解决方案。你想要什么很难说。至少,我无法理解这个问题。
-
问题是我想知道是否有替代方案,Thread.Sleep(游戏冻结)、Tasks(FW4)或 Yield 返回新的 WaitForSeconds(out 和 ref 与迭代器不兼容在这种类型的退货中使用)