Unity登录按钮

时间:2015-03-11 10:15:41

标签: c# unity3d unityscript

更新修复

我设法通过重新格式化代码来修复我的问题,并且使用正确的IEmu函数这里是固定代码

using UnityEngine;
using System.Collections;

public class Click : MonoBehaviour 
{
    public static string user = "", name = "";
    private string password = "", rePass = "", message = "";
    public UnityEngine.UI.Text userString;
    public UnityEngine.UI.Text passString;
    private bool register = false;


    public int CurrentQuestion;
    int levelChanger;

    void Start(){
        levelChanger = 1;
        print (CurrentQuestion);
    }

    public void Clicked()
    {
        if (message != "")
            print (message);


            PlayerPrefs.SetInt ("CurrentQ",levelChanger);
            message = "";

            if (userString.text == "" || passString.text == "")
                message += "Please enter all the fields \n";
            else
            {
                WWWForm form = new WWWForm();
                form.AddField("user", userString.text);

                form.AddField("password", passString.text);
                WWW w = new WWW("localhost", form);
                StartCoroutine(login(w));
        }



    }


IEnumerator login(WWW w)
{
    yield return w;
    if (w.error == null)
    {
        if (w.text == "login-SUCCESS")
        {
            PlayerPrefs.SetString("Username",userString.text);
            Application.LoadLevel(1);
            print("WOOOOOOOOOOOOOOO!");
        }
        else
            message += w.text;
    }
    else
    {
        message += "ERROR: " + w.error + "\n";
    }
}

}

我有这个问题不断出现,每当我尝试让我的登录工作它与GUILayout一起使用但是当我尝试将其更改为一个canvased系统时,我遇到了一些错误。 这些错误是

InvalidOperationException: operation is not valid due to the current state of object.... 

这是我的代码

using UnityEngine;
using System.Collections;

public class database : MonoBehaviour 
{
    public static string user = "", name = "";
    private string password = "", rePass = "", message = "";
    public UnityEngine.UI.Text userString;
    public UnityEngine.UI.Text passString;
    public UnityEngine.UI.Button LoginClick;
    private bool register = false;

    private void OnGUI()
    {
        if (message != "")
            GUILayout.Box(message);

        if (register)
        {
            GUILayout.Label("User");
            user = GUILayout.TextField(user);
            GUILayout.Label("Name");
            name = GUILayout.TextField(name);
            GUILayout.Label("password");
            password = GUILayout.PasswordField(password, "*"[0]);
            GUILayout.Label("Re-password");
            rePass = GUILayout.PasswordField(rePass, "*"[0]);

            GUILayout.BeginHorizontal();

            /*if (GUILayout.Button("Back"))
                register = false;

            if (GUILayout.Button("Register"))
            {
                message = "";

                if (user == "" || name == "" || password == "")
                    message += "Please enter all the fields \n";
                else
                {
                    if (password == rePass)
                    {
                        WWWForm form = new WWWForm();
                        form.AddField("user", user);
                        form.AddField("name", name);
                        form.AddField("password", password);
                        WWW w = new WWW("http://f6-preview.awardspace.com/unitytutorial.com/register.php", form);
                        StartCoroutine(registerFunc(w));
                    }
                    else
                        message += "Your Password does not match \n";
                }
            }*/

            //GUILayout.EndHorizontal();
        }
        else
        {

            user = userString.ToString ();
            password = passString.ToString ();

            if (LoginClick)
            {
                message = "";
                PlayerPrefs.SetString ("MyUser",user);

                if (user == "" || password == "")
                    message += "Please enter all the fields \n";
                else
                {
                    WWWForm form = new WWWForm();
                    form.AddField("user", user);
                    form.AddField("password", password);
                    WWW w = new WWW("DuntbeSilly:3", form);
                    StartCoroutine(login(w));
                }
            }

            //if (GUILayout.Button("Register"))
            //  register = false;

            GUILayout.EndHorizontal();
            GUILayout.EndArea();
        }
    }

    IEnumerator login(WWW w)
    {
        yield return w;
        if (w.error == null)
        {
            if (w.text == "login-SUCCESS")
            {
                PlayerPrefs.SetInt ("CurrentQ",Score.CurrentQuestion +=1);
                Application.LoadLevel(PlayerPrefs.GetInt("CurrentQ"));
                print ("changelevel");
            }
            else
                message += w.text;
        }
        else
        {
            message += "ERROR: " + w.error + "\n";
        }
    }

    IEnumerator registerFunc(WWW w)
    {
        yield return w;
        if (w.error == null)
        {
            message += w.text;
        }
        else
        {
            message += "ERROR: " + w.error + "\n";
        }

    }
}

0 个答案:

没有答案