gpt4 book ai didi

c# - C# 中的 Unity GUI TextField

转载 作者:太空狗 更新时间:2023-10-30 01:36:06 24 4
gpt4 key购买 nike

所以我在 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 documentation 的注释关于 text 参数:

Text to edit. The return value of this function should be assigned back to the string as shown in the example.

尝试将您的代码更改为:

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

关于c# - C# 中的 Unity GUI TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com