【问题标题】:unity startcoroutine not work in startunity startcoroutine 在启动时不起作用
【发布时间】:2016-07-04 11:54:15
【问题描述】:

嗨,这是我的代码。

Unity通过PHP代码连接mysql。

public string userLoginFile = "localhost:8088/database/Userlogin.php?";
string username = "****";
string password = "****";
string Name;
 bool InfoTest = false;
string family;
string status;
 Text we;
 Text st;
public void Start(){
    GameObject inputUsernameFiled = GameObject.Find ("usernameInput");
    GameObject inputPasswordFiled = GameObject.Find ("passwordInput");
    GameObject welcomeQ = GameObject.Find ("welcome");
    GameObject statusQ = GameObject.Find ("status");
    we = welcomeQ.GetComponent<Text> ();
    st = statusQ.GetComponent<Text> ();

    InputField In1 = inputUsernameFiled.GetComponent<InputField> ();
    InputField In2 = inputPasswordFiled.GetComponent<InputField> ();


        //st.text = "Your Account Is Saved!";

    if(PlayerPrefs.GetFloat ("InfoCheck") == 1){
        username = In1.text;
        password = In2.text;
        we.text = "Welcome To Golden3 Service!";
        //st.text = "Your Status Is: " + status;
    }
    if(PlayerPrefs.GetFloat ("InfoCheck") == 2){
        username = PlayerPrefs.GetString ("UsernameIs");
        password = PlayerPrefs.GetString ("PasswordIs");
        StartCoroutine (LoginUser (username, password));
        st.text = "Your Account Is Connect!";
        we.text = "Welcome "+ Name +" To Golden3 Service!";
        Debug.Log (Name);
    }
}

void Update(){
    //Awake ();
}

public void OnClick(){
    if(PlayerPrefs.GetFloat ("InfoCheck") == 1){
    PlayerPrefs.SetString ("UsernameIs", username);
    PlayerPrefs.SetString ("PasswordIs", password);
    PlayerPrefs.SetFloat ("InfoCheck", 2);
    StartCoroutine (LoginUser (username, password));
    if(InfoTest == true){
        st.text = "Your Status Is: Connectet";
            Debug.Log (Name);
    }//end if

    }//end if
}// end click function

 IEnumerator LoginUser(string user, string pass){
    WWW login = new WWW(userLoginFile + "username=" + user + "&password=" + pass);
    yield return login;

    if(login.error == null){
        InfoTest = true;
        Name = "test";
        /*
        string[] credentials = login.text.Split('/');
        foreach (string str in credentials){
            string[] cred= str.Split('=');

            for(int i =0; i< cred.Length; i++) {
                if(cred[i] == "name")
                    Name = cred[i+1];
                if(cred[i] == "family")
                    family = cred[i+1];
                    status = "Logged In!";
            }

        }*/
    }//end if
}//end login user

在start函数中调用startcoroutine时,不起作用

但是当我点击按钮时,startcoroutine Works!

请帮帮我!

谢谢。

【问题讨论】:

  • 你调试了吗?我不怀疑 ;) 只需使用 Debug.Log 并检查发生了什么——我也不确定你对 Start 有什么期望——它只会被调用一次——在 GameObject 生命的开始——不再。

标签: unity3d unityscript unity5


【解决方案1】:
  • 在启动方法调用协程可能有两个原因
  • 无法访问该代码以在 Start 中调用 Coroutine
  • 由于用户名和密码错误,导致 URL 无效
if(PlayerPrefs.HasKey("UsernameIs")){ 用户名 = PlayerPrefs.GetString ("UsernameIs"); }别的{ 用户名="****"; } if(PlayerPrefs.HasKey("PasswordIs")){ 密码 = PlayerPrefs.GetString ("PasswordIs"); }别的{ 密码="****"; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2020-05-19
    相关资源
    最近更新 更多