用户界面-C#中的UnityGUITextField



所以我在Unity中制作了一个注册场景,当我使用这个脚本放置文本字段和按钮时,但当我播放时,我无法键入文本字段。我的代码出了什么问题?

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);
    }
}

将输入变量存储为类/脚本的成员,而不是方法。每一帧,你都会将其重置回一个空字符串,删除用户试图输入的内容。

Unity3D文档中关于text参数的注释:

要编辑的文本。应指定此函数的返回值返回到示例中所示的字符串。

尝试将代码更改为:

//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);
    }
}

相关内容

  • 没有找到相关文章

最新更新