2015-06-29 103 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版本。

相关问题