[英]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
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.