【发布时间】:2015-05-24 06:05:54
【问题描述】:
为什么带有 yield 的方法永远不会执行?永远无法达到 Debug.Log o 首次调用方法!
public IEnumerator call(string method,WWWForm postData,Action<string> callback) {
Debug.Log("call");
WWW www = new WWW(this.apiUrl + method,postData);
yield return www;
Debug.Log("www ok");
callback(www.text);
}
public IEnumerator call2(string method,WWWForm postData,Action<string> callback) {
Debug.Log("call2");
return null;
}
public void login(string email,string password,Action<string> callback)
{
Debug.Log("login");
WWWForm form = new WWWForm();
form.AddField("email",email);
form.AddField("password",password);
Debug.Log("->playerLogin");
this.call2("playerLogin", form,callback);
this.call("playerLogin", form,callback);
Debug.Log("<-playerLogin");
}
【问题讨论】:
-
你永远不会迭代
call返回的IEnumerator,所以代码不会运行。 -
Answer to stackoverflow.com/questions/13424485/… 涵盖了迭代器何时执行(问题有点不同,询问为什么某些代码永远不会运行,但答案涵盖了两者)。