Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 团结等待了几秒钟_C#_Unity3d - Fatal编程技术网

C# 团结等待了几秒钟

C# 团结等待了几秒钟,c#,unity3d,C#,Unity3d,我试图创建一个简单的飞溅屏幕。起初,这个waitfors可以工作几秒钟,但几周后,这个代码突然不想工作了。我尝试使用debug.log,但是我只得到了“before wait”和“after wait”,即使在5分钟后也没有出现。我使用的是unity 4.5,为什么会出现这种情况 void Start () { guiTexture.pixelInset = new Rect (0, 0, Screen.width, Screen.height); StartCoroutin

我试图创建一个简单的飞溅屏幕。起初,这个waitfors可以工作几秒钟,但几周后,这个代码突然不想工作了。我尝试使用debug.log,但是我只得到了“before wait”和“after wait”,即使在5分钟后也没有出现。我使用的是unity 4.5,为什么会出现这种情况

void Start () {
     guiTexture.pixelInset = new Rect (0, 0, Screen.width, Screen.height);
     StartCoroutine (LoadNextScene ());
 }
 IEnumerator LoadNextScene(){
     Debug.Log ("before wait");
     yield return new WaitForSeconds (DelayTime);
     Debug.Log ("after wait");
     if (NextLevel != "") {
         Application .LoadLevel (NextLevel );    
     }
 }

你的代码是完美的。这意味着两件事之一:

  • 延迟时间过大(调试完毕) 或者,更有可能:
  • 游戏对象被销毁,因此协同程序不再运行
  • 试试这个:

    void Start () {
         guiTexture.pixelInset = new Rect (0, 0, Screen.width, Screen.height);
         StartCoroutine (LoadNextScene ());
     }
     IEnumerator LoadNextScene(){
         Debug.Log ("before wait. Pausing for seconds: " + DelayTime);
         yield return new WaitForSeconds (DelayTime);
         Debug.Log ("after wait");
         if (NextLevel != "") {
             Application .LoadLevel (NextLevel );    
         }
     }
    
    void OnDestroy() {
            Debug.Log("Destroyed");
    }
    

    如果你真的需要等几秒钟。您可以只使用Invoke而不是Co例程

    直到现在,我仍然不明白为什么会有人在简单的基础上使用Co例程

    Co例程通常用于让它在后台完成自己的工作,而其他例程仍在运行

    检查游戏的在线数据。等下载资产

    这是使用Co例程的最佳方式

    但是,当您只是想延迟一些调用时,请使用Invoke

     void Start(){
            Invoke("LoadMyScene",2);
            // LoadMyScene will be called after 2 seconds.
     }
    
    
     public void LoadMyScene(){
       Application.LoadLevel(YourLevelScenehere);
    
    }
    

    更新

    只是为了巩固@peterept给出的答案,以下可能是导致您的协同程序在
    等待秒
    上卡住的原因:

  • WaitForSeconds
    中给出的时间太长。虽然这是一个愚蠢的理由,但有时这种情况会发生
  • 从中调用协同例程的对象正在被销毁。您可以按如下方式进行检查

    void OnDestroy() {
        Debug.Log("Destroyed");
    }
    
  • 在任何地方,都会调用
    StopAllCoroutines()
    ,这将停止您的coroutine
  • 最后,检查anywhere
    Time.timeScale
    是否设置为0。因为当它为零时,它会停止增加
    Time.deltaTime
    ,因此
    WaitForSeconds
    永远不会达到指定的值。您可以在
    Edit->Project settings->Time
    中检查当前的时间刻度参数。或者在调用协同程序之前通过日志记录来检查其值

  • 最后一个也是我面临的问题,@peterept comment在这方面帮助了我。

    我尝试了你的代码,我用2秒填充DelayTime,得到“before wait.Pausing for seconds:2”。然后什么也没发生。你知道吗?你没有看到“销毁的”日志?事实上,有第三种选择,如果你的代码在某处调用“StopAllCoroutines()”不,我没有看到销毁的日志,也没有任何代码来停止所有的coutroutines..好的,有第四种(希望是最后一种)选择,你必须将Time.timeScale设置为0(这会阻止Time.deltaTime增加,并阻止WaitForSeconds达到延迟时间)。现在,假设您使用Time.timeScale=0有一个有效的原因,那么您必须使用类似Time.realtimeSinceStartup的内容重新编码延迟。