C# 中的 Unity GUI 文本字段

Posted

技术标签:

【中文标题】C# 中的 Unity GUI 文本字段【英文标题】:Unity GUI TextField in C# 【发布时间】:2014-04-21 01:42:06 【问题描述】:

所以我在 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);
    

【问题讨论】:

【参考方案1】:

将输入变量存储为类/脚本的成员,而不是方法。您将每一帧重置为一个空字符串,从而清除用户尝试输入的内容。

Unity3D documentation 中关于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);
    

【讨论】:

@DannikaRodriguez 如果它戴了,您应该选择此答案旁边的 Tick 以表明它是您问题的正确答案。【参考方案2】:

大声笑,你把字符串变量设置为“”,它会一直这样。

正确方法:

string text;

text = GUILayout.TextField(text);

您可以阅读我创建的用于创建自定义可编写脚本的对象检查器 GUI 的代码。 观看和学习!

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(CursorPreset))]
public class CursorGUI : Editor

// General
public Texture2D cursorTexture;

// Info
public string displayName;

// Target Window
public PreviewWindow previewWindow;

public override void OnInspectorGUI()

    // Target
    CursorPreset _target = (CursorPreset)target;

    // Main GUI
    #region Main GUI

    // Gets the cursor texture
    cursorTexture = (Texture2D)EditorGUI.ObjectField(new Rect(-628, 4, 700, 70), "", cursorTexture, typeof(Texture2D));

    GUILayout.BeginHorizontal();

    GUILayout.Space(60);

    // Gets the preset display name
    displayName = EditorGUILayout.TextField("Display Name:", displayName);

    GUILayout.EndHorizontal();

    GUILayout.Space(11);

    GUILayout.BeginHorizontal();

    GUILayout.Space(60);

    // If the 'Save' button is clicked
    if (GUILayout.Button("Save"))
    
        Debug.Log("Saved Successfully!");

        Save(_target); // Save the current preset
    

    GUILayout.EndHorizontal();


    GUILayout.BeginHorizontal();

    GUILayout.Space(60);

    // If the 'Reset' button is clicked
    if (GUILayout.Button("Reset"))
    
        Reset(_target); // Resets all data
    

    // If the 'Preview' button is clicked
    if (GUILayout.Button("Preview"))
    
        // Previews the final texture
        PreviewTexture(_target); // Preview
        Save(_target); // Save
        PreviewTexture(_target); // Preview
    

    GUILayout.EndHorizontal();

    GUILayout.Space(70);

    #endregion


/// <summary>
/// Saves the current preset
/// </summary>
private void Save(CursorPreset target)

    // Saves to the preset
    target.cursorDefault = cursorTexture; // Cursor Texture

    target.displayName = displayName; // Display Name

    // Saves to the preview
    previewWindow.cursorTexture = cursorTexture; // Cursor Texture

    previewWindow.displayName = displayName; // Display Name


/// <summary>
/// Previews the final cursor texture
/// </summary>
private void PreviewTexture(CursorPreset target)

    previewWindow = PreviewWindow.ShowWindow();


/// <summary>
/// Resets all the data
/// </summary>
private void Reset(CursorPreset target)

    // Saves to the local variables
    displayName = ""; // Display Name

    cursorTexture = null; // Cursor Texture

    // Saves to the preset
    Save(target);


private void Awake()

    // Prevents the preset to be reset after entering Play Mode and restores all values
    CursorPreset _target = (CursorPreset)target;

    cursorTexture = _target.cursorDefault; // Restores the cursor texture
    displayName = _target.displayName; // Restores the display name


结果: result

【讨论】:

以上是关于C# 中的 Unity GUI 文本字段的主要内容,如果未能解决你的问题,请参考以下文章

尝试在java中的GUI中添加表格下方的文本字段

当我在 xcode 中构建游戏时,所有 gui 纹理/文本看起来都不同(unity3d)

如何通过单击tkinter中不同窗口中的按钮来获取位于窗口中的GUI文本字段的参数? [重复]

csharp Unity GUI文本区:`MLMEditorGUI.TextAreaWithTabs(Rect位置,字符串文本)`

从 Firebase 中的实时数据库获取数据后,无法写入 Unity 的 TMPro 文本字段

在unity5中,用c#脚本如何获取UGUI中Input field中的文本?