如何正确使用Coroutines和Callback?从 IEnumerator 中检索数组

How to use Coroutines and Callback properly? Retrieving an array out of an IEnumerator

我们在使用 IEnumerator 时遇到一些问题,不知道如何从中检索数组。我们发现您必须使用回调来完成它,但我们真的不知道如何使用它。这是 IEnumerator 的代码和需要从中接收字符串数组的 void。

public void StartRoutineGetProjects(string username, string password, string url){
    StartCoroutine(GetProjects(username, password, url));

    // here we dont know how to receive the array, need some help here
}

public IEnumerator GetProjects (string username, string password, string url, Action<string[]> callback)
{
    string privateURL = "http://" + url + "/Unity/myprojects.php";

    WWWForm form = new WWWForm ();
    form.AddField ("username", username);
    form.AddField ("password", password);

    // Send WWWForm
    WWW projects_get = new WWW (privateURL, form);

    if (projects_get.error != null && projects_get.error != "") {
        Debug.Log ("Internal Error");
    } else {
        // splitting the result at "|"
        string[] tempProjects = projects_get.text.Split ("|".ToCharArray ());

        yield return tempProjects;

        callback(tempProjects) // <-- here we want to return the array

    }
}

如果能得到任何帮助,我们会很高兴。

StartCoroutine(GetProjects(username, password, url, (tempProjectArray) => 
{
    // do stuff with your project array
}));

void OnProjectRetrieved(string[] projects)
{
    // do stuff with your project array
}

public void StartRoutineGetProjects(string username, string password, string url)
{
    StartCoroutine(GetProjects(username, password, url, OnProjectRetrieved));
}

非常感谢你的帮助,我按照你说的修改了我的代码。 不幸的是,我在第一个 post 中遗漏了一些东西。 void 函数也是一个 string[] 函数,它将数组传递给另一个脚本。这是代码,现在看起来如何。我必须以某种方式 return 字符串 [],但我不知道具体在哪里做。

    public string[] StartRoutineGetProjects(string username, string password, string url){
    string[] temp;

    StartCoroutine(GetProjects(username, password, url,(stringArray)=>{
        temp = stringArray;
    }));
}

public IEnumerator GetProjects (string username, string password, string url, Action<string[]> callback)
{
    string privateURL = "http://" + url + "/Unity/myprojects.php";

    WWWForm form = new WWWForm ();
    form.AddField ("username", username);
    form.AddField ("password", password);

    // send WWWForm
    WWW projects_get = new WWW (privateURL, form);

    // Receiving projects
    yield return projects_get;

    if (projects_get.error != null && projects_get.error != "") {
        Debug.Log ("Interner Fehler");
        callback (null);
    } else {
        // splitting the result at "|"
        string[] tempProjekte = projects_get.text.Split ("|".ToCharArray ());

        callback (tempProjekte);
    }
}

[已解决] 是的,终于成功了。非常感谢你。我们不想保留我们的解决方案,以便其他人可以从中受益。 所以这是代码。 首先是数据库脚本:

        public void StartRoutineCheckLoginCorrect (string username, string password, string url, Action<string[]> callback)
    {
        StartCoroutine (Login (username, password, url, callback));
    }

    IEnumerator Login (string username, string password, string url,Action<string[]> callback)
    {
        string loginURL = "http://" + url + "/Unity/mylogin.php";
        WWWForm form = new WWWForm ();
        form.AddField ("username", username);
        form.AddField ("password", password);

        WWW users_get = new WWW (loginURL, form);

        yield return users_get;

        if (users_get.error != null && users_get.error != "") {
            Debug.Log ("Login failed");

        } else {
            string[] temp = users_get.text.Split ("*".ToCharArray ());

            if (temp.Length <= 2 || temp [0].ToString () == "Username or password false") {
                Debug.Log (temp [0].ToString ());
                login = false;
            } else {
                Debug.Log ("Login succeeded");
                login = true;
                callback (temp);
            }
        }
    }

    public void StartRoutineGetProjects(string id, string username, string url, Action<string[]> callback){
        StartCoroutine (GetProjects (id, username, url,callback));
    }

    public IEnumerator GetProjects (string id, string username, string url, Action<string[]> callback)
    {
        string privateURL = "http://" + url + "/Unity/myprojects.php";

        WWWForm form = new WWWForm ();
        form.AddField ("id", id);
        form.AddField ("username", username);

        WWW projects_get = new WWW (privateURL, form);

        yield return projects_get;

        if (projects_get.error != null && projects_get.error != "") {
            Debug.Log ("Internal error");
            callback (null);
        } else {
            string[] tempProjects = projects_get.text.Split ("|".ToCharArray ());

            callback (tempProjects);

        }
    }

第二个登录脚本:这里我们从数据库脚本中获取对变量的访问权限。

    public void LoginStart ()
{
    StartCoroutine (Login ());
}

IEnumerator Login ()
{
    userName = inputUsername.text;
    password = inputPassword.text;

    string[] userData = null;
    bool wait2 = true;
    dbscript.StartRoutineCheckLoginCorrect (userName, password, url,(callback) =>{
        userData = callback;
        wait2 = false;
    });

    while (wait2) {
        yield return null;
    }

    id = userData [0];

    string[] stringArray = null;
    bool wait = true;
    dbscript.StartRoutineGetProjects (id, userName, url, (callback) => {
        stringArray = callback;
        wait = false;
    });