簡體   English   中英

使用C#從Unity3D的輸入字段中獲取文本

[英]Get text from Input field in Unity3D with C#

我正在嘗試使用C#Unity3D的inputField中獲取文本。

我在我的編輯器中放置了一個inputField,重命名並標記為: Username_field

我的問題是:如何在C#腳本中獲取InputField Username_field中的文本?

將monobehaviour腳本附加到InputField gameObject:

public class test : MonoBehaviour {
    void Start ()
    {
        var input = gameObject.GetComponent<InputField>();
        var se= new InputField.SubmitEvent();
        se.AddListener(SubmitName);
        input.onEndEdit = se;

        //or simply use the line below, 
        //input.onEndEdit.AddListener(SubmitName);  // This also works
    }

    private void SubmitName(string arg0)
    {
        Debug.Log(arg0);
    }
}

另見動畫:

在此輸入圖像描述

您可以使用InputField的“On Value Change”或“End Edit”事件。

Unity3D文檔提供了有關如何使用UnityEvent的更多詳細信息: http ://docs.unity3d.com/Manual/UnityEvents.html

或者,您還應該能夠使用InputField附加到的Text控件Text屬性來訪問Text。

using UnityEngine.UI;
public InputField betInput;
void Start () {
    betInput.onEndEdit.AddListener(delegate { inputBetValue(betInput); });
}
public void inputBetValue(InputField userInput)
{
    betValue = int.Parse(userInput.text);
    Debug.Log(userInput.text);
}

https://i.stack.imgur.com/CeF1a.png //這是您選擇方法的統一圖片。 還值得一試https://unity3d.com/learn/tutorials/topics/scripting/text-input

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM