将字符串从 IEnumerator 传递到 void

passing string from IEnumerator to void

我正在尝试将字符串从 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 代码工作正常

谢谢希望有用如果需要了解更多请询问

您需要让 void 函数接受这样的字符串:

public void ResisterButton (string str){   

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

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

您必须等待协程完成才能使用 userDataString 结果。即使您没有在 CheackDBUN 中更改 userDataString(我认为这是您的意图),您仍然可能会看到 User Data String in ResisterButtonUser 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);
}