2017-12-08 2 views
0

abrufen, haben wir Probleme mit einem IEnumerator und wissen nicht, wie wir ein Array daraus abrufen können. Wir haben herausgefunden, dass Sie es mit einem Rückruf tun müssen, aber wir wissen nicht wirklich, wie wir es verwenden sollen. Hier ist der Code des IEnumerator und der void, die ein String-Array von ihm erhalten müssen.Wie werden Coroutinen und Callback richtig verwendet? Wenn wir ein Array aus einem IEnumerator

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 

    } 
} 

Wir wären froh für jede Hilfe, die wir bekommen können.

Antwort

0

[Gelöst] Ja endlich funktioniert es. Vielen Dank. Wir möchten unsere Lösung nicht zurückhalten, damit andere davon profitieren können. Also hier ist der Code. Zuerst wird die 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); 

     } 
    } 

Zweiter des Loginscript: Hier erhalten wir Zugang zu den Variablen aus der 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; 
    }); 
0
StartCoroutine(GetProjects(username, password, url, (tempProjectArray) => 
{ 
    // do stuff with your project array 
})); 

oder

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

Vielen Dank für Ihre Hilfe sehr, änderte ich meinen Code, wie Sie gesagt haben. Leider habe ich etwas in meinem ersten Beitrag verpasst. Die void-Funktion ist ebenfalls eine string [] - Funktion, die das Array an ein anderes Skript übergeben soll. Hier ist der Code, wie es jetzt aussieht. Ich muss die Zeichenfolge [] irgendwie zurückgeben, aber ich weiß nicht genau, wo ich das machen soll.

Verwandte Themen