2015-06-29 88 views
0

我正在製作一段代碼,它從MySQL獲取一條信息並將其顯示在UI上。問題是,程序不會等待MySQL查詢完成並直接顯示變量(由於查詢結果沒有按時完成,因此該變量爲空)如何在繼續之前等待StartCoroutine()完成

粗略提綱我的代碼是:

bool notYetDone = true; 

StartCoroutine(query(web)); 

IEnumerator query (WWW web){ 
    yield return web; 
    while(notYetDone == true){ 
     yield return new WaitForSeconds(1f); 
     if(web.error == null){ 
      //no problems with the query, some code here 
      notYetDone = false; 
     } else if (web.error != null){ 
      //some code here for handling errors 
     } else { 
      Debug.Log("i dont really know why we reached here"); 
     } 
    } 
} 

東西我還注意到的是,它似乎改變的notYetDone值,並立即結束循環。我的代碼有問題嗎?提前致謝。

+0

嘗試使用web.isDone而不是notYetDone == true –

+0

直接用web.isDone替換while參數似乎會產生無限循環。但生病嘗試使用這種方法。感謝您的建議 – ryuuuuuusei

+0

如果您使用:while(!web.isDone),那麼當它完成時它將是true並退出循環。 –

回答

2

嘗試:

IEnumerator query (WWW web) 
{ 
    //yield return web; 
    while(!web.isDone && web.Error == null) 
    { 
    //this will loop until your query is finished 
    //do something while waiting... 
     yield return null; 
    } 
    if(web.Error != null) 
    Debug.Log("Errors on web"); 

} 
+0

這是不正確的,原因有兩個: 收益率回報網頁; 已足以等待www例程的結束,因此while循環無用,但如果編碼器忘記在Web上放棄之前,它將阻塞該線程直到完成。確保你應該完全刪除它,或者在 –

+0

內部產生空值......你說的沒錯,或者你可以刪除yield return web,並且做一些類似while(!web.isDone && web。錯誤== null){yield return null} if(error!= null){Debug.Log(「Web上的錯誤」)},while循環只是在萬一他想在查詢運行時做些什麼... –

+0

現在它是正確的 –

2

試試這個:

class QueryBehaviour: MonoBehaviour 
{ 
    bool queryFinished = false; 
    WWW wwwQuery; 

    IEnumerator Query() 
    { 
    wwwQuery = new WWW("url_to_query"); 
    yield return wwwQuery; 

    queryFinished = true; 
    //results or error should be here 
    } 

    Update() 
    { 
    if(queryFinished == false) 
    { 
     return; 
    } 
    else 
    { 
     //use wwwQuery here 
    } 
    } 
} 

然後就叫查詢

注意:如果您致電收益率回報wwwQuery,則不需要等待。如果你不想這樣做,你應該忙於等待,例如,你想檢查進度下載,在這種情況下,你應該在更新方法MonoBehaviour中查詢www類的結果。

+0

抱歉,但它仍然繼續下一行代碼。 – ryuuuuuusei

+0

好的,現在我明白了,是的,它會轉到下一行代碼。該操作是異步的。你需要在收益率後設置一個標誌,然後你應該有你的查詢 –

+0

你能解釋這個流程嗎? – ryuuuuuusei

0

配售產量關鍵字之前startcoroutine也有同樣的效果。你的情況:

yield StartCoroutine(query(web)); 
//at this point it is guaranteed to be completed 

http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

+0

但是,你需要已經在協同程序中。也許你正在考慮'IEnumerator Start(){}' –

+0

yeild只能用在Couroutine裏面! –

0

有關使用回調如何?

public void showMessage(string message) 
    { 
     setMessage(message); 
     Debug.Log("start_fadeIn"); 
     StartCoroutine(coroutine__fadeIn(delegate 
     { 
      Debug.Log("start_fadeOut"); 
      StartCoroutine(coroutine__fadeOut(delegate 
      { 
       Debug.Log("done"); 
      })); 
     })); 
    } 


    private IEnumerator coroutine__fadeIn(Action completion) 
    { 
     CanvasGroup canvasGroup = GetComponent<CanvasGroup>(); 
     for (float f = 0f; f <= 1; f += 0.01f) 
     { 
      canvasGroup.alpha = f; 
      yield return null; 
     } 

     completion(); 
    } 

    private IEnumerator coroutine__fadeOut(Action completion) 
    { 
     CanvasGroup canvasGroup = GetComponent<CanvasGroup>(); 
     for (float f = 1f; f >= 0; f -= 0.01f) 
     { 
      canvasGroup.alpha = f; 
      yield return null; 
     } 

     completion(); 
    } 

警告,這種方式需要使用支持行動一流的.NET版本。

相關問題