【问题标题】:How to use Coroutines and Callback properly? Retrieving an array out of an IEnumerator如何正确使用协程和回调?从 IEnumerator 中检索数组
【发布时间】:2017-12-08 09:28:37
【问题描述】:

我们在使用 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

    }
}

我们会很高兴得到每一个帮助。

【问题讨论】:

    标签: c# arrays callback delegates ienumerator


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      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));
      }
      

      【讨论】:

        【解决方案3】:

        非常感谢您的帮助,我按照您所说的更改了代码。 不幸的是,我在第一篇文章中遗漏了一些东西。 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);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2019-02-11
          • 1970-01-01
          • 1970-01-01
          • 2018-04-27
          • 2020-09-28
          • 2021-01-14
          • 1970-01-01
          • 2020-04-08
          • 1970-01-01
          相关资源
          最近更新 更多