Unity GUI TextField in C #

so I am recording in Unity, and when I used this script to place text fields and buttons, but when I play, I cannot enter a text field. What is wrong with my code?

void OnGUI () {

    string email = "";
    string username = "";
    string password = "";
    string confirm = "";

    email = GUI.TextField (new Rect (250, 93, 250, 25), email, 40);
    username = GUI.TextField ( new Rect (250, 125, 250, 25), username, 40);
    password = GUI.PasswordField (new Rect (250, 157, 250, 25), password, "*"[0], 40);
    confirm = GUI.PasswordField (new Rect (300, 189, 200, 25), confirm, "*"[0], 40);

    if (GUI.Button (new Rect (300, 250, 100, 30), "Sign-up")) {
        Debug.Log(email + " " + username + " " + password + " " + confirm);
    }
}
+4
source share
1 answer

Save the input variables as a member of your class / script, not a method. Each reset frame returns to an empty line that deletes what the user tried to enter.

Note the Unity3D documentation regarding the parameter text:

Text to edit. The return value of this function must be assigned. Return to the line, as shown in the example.

Try changing your code to:

//notice these are pulled out from the method and now attached to the script
string email = ""; 
string username = "";
string password = "";
string confirm = "";

void OnGUI () {

    email = GUI.TextField (new Rect (250, 93, 250, 25), email, 40);
    username = GUI.TextField ( new Rect (250, 125, 250, 25), username, 40);
    password = GUI.PasswordField (new Rect (250, 157, 250, 25), password, "*"[0], 40);
    confirm = GUI.PasswordField (new Rect (300, 189, 200, 25), confirm, "*"[0], 40);

    if (GUI.Button (new Rect (300, 250, 100, 30), "Sign-up")) {
        Debug.Log(email + " " + username + " " + password + " " + confirm);
    }
}
+3
source

Source: https://habr.com/ru/post/1537320/


All Articles