如何正确使用协同程序和回调?从IEnumerator中检索数组

时间:2017-12-08 09:28:38

标签: c# arrays callback delegates 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

    }
}

我们很乐意为您提供一切帮助。

3 个答案:

答案 0 :(得分:0)

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));
}

答案 1 :(得分:0)

非常感谢您的帮助,我按照您的说法更改了我的代码。 不幸的是我在第一篇文章中遗漏了一些东void函数也是一个string [] - 函数,它应该将数组传递给另一个脚本。这是代码,它现在看起来如何。我必须以某种方式返回字符串[],但我不确切知道在哪里这样做。

    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);
    }
}

答案 2 :(得分:0)

[求助]是的,终于可行了。非常感谢你。我们不想隐瞒我们的解决方案,因此其他人可以从中受益。 所以这是代码。 首先是databasescript:

        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);

        }
    }

Loginscript:在这里我们可以访问来自databasescript的变量。

    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;
    });