为什么带有 yield 的方法永远不会执行?

Why a method with yield never execute?

为什么带有 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() 返回的 Enumerator,因此没有执行 yield 方法。

yield 是延迟执行的。这意味着它只会在实际需要时执行,在迭代时:

var enumerable = this.call("playerLogin", form,callback);
enumerable.GetEnumerator().MoveNext(); // will be executed here