C#中的Unity GUI TextField

时间:2014-04-21 01:42:07

标签: c# user-interface unity3d

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

lol,你把字符串变量设置为“”,它会保持这样。

正确方法:

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