将字符串从IEnumerator传递给void

时间:2017-07-22 00:47:30

标签: c# unity3d void ienumerator

我试图将一个字符串从IEnumerator传递给void。但字符串不断出现null :(这是我的代码:我是新手,所以请帮助谢谢大家。

我的虚空

public void ResisterButton ()
{   
    //checked Username
    if (Username != "") {
        StartCoroutine (CheackDBUN (Username));
        Debug.LogWarning("User Data String in ResisterButton " + userDataString); // not work is empty :(


    // do stuff with text eg turn bool UN = true

更多与此无关的代码

和IEnumerator

IEnumerator CheackDBUN(string UsernamePost ){
    WWWForm form = new WWWForm ();
    form.AddField ("usernamePost", UsernamePost);

    WWW userData = new WWW(checkUsernameURL, form);
    yield return userData;
    string userDataString = userData.text;
    Debug.LogWarning("User Data String in CheackDBUN " + userDataString); //Working has text
    yield return (userDataString);
}

Mysql和php代码都运行良好

感谢希望,如果需要了解更多,请致电

2 个答案:

答案 0 :(得分:1)

你需要让void函数接受一个这样的字符串:

public void ResisterButton (string str){   

然后将用户数据字符串传递给它:

Debug.LogWarning("User Data String in CheackDBUN " + userDataString); //Working has text
ResisterButton(userDataString);
yield return (userDataString);

答案 1 :(得分:0)

在使用userDataString结果之前,您必须等待协程完成。即使您未在userDataString中更改CheackDBUN(我认为这是您的意图),您仍然可能会看到User Data String in ResisterButton之前已记录User Data String in CheackDBUN

您可以使用回调委托告诉协程一旦完成后如何处理userDataString

public void ResisterButton ()
{    
    if (Username != "")
    {           
        StartCoroutine(CheackDBUN(Username,
            (userDataString) =>
            {
                Debug.LogWarning("User Data String in ResisterButton " + userDataString); 
                // do stuff with text eg turn bool UN = true
            }
            ));
    }
}


IEnumerator CheackDBUN(string UsernamePost, Action<string> callback){
    WWWForm form = new WWWForm ();
    form.AddField ("usernamePost", UsernamePost);

    WWW userData = new WWW(checkUsernameURL, form);
    yield return userData;
    string userDataString = userData.text;
    Debug.LogWarning("User Data String in CheackDBUN " + userDataString); //Working has text
    callback(userDataString);
}