[英]Get text from Input field in Unity3D with C#
I'm trying to get a text inside an inputField in Unity3D
with C#
. 我正在尝试使用C#
在Unity3D
的inputField中获取文本。
I've placed an inputField in my editor, renamed and tagged in: Username_field . 我在我的编辑器中放置了一个inputField,重命名并标记为: Username_field 。
My question is: How i can get the text inside the InputField
Username_field in a C#
script? 我的问题是:如何在C#
脚本中获取InputField
Username_field中的文本?
Attach below monobehaviour script to your InputField gameObject: 将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);
}
}
See also below animation: 另见动画:
You can use the "On Value Change" or "End Edit" event of the InputField . 您可以使用InputField的“On Value Change”或“End Edit”事件。
The Unity3D documentation provides more detail on how to use a UnityEvent: http://docs.unity3d.com/Manual/UnityEvents.html Unity3D文档提供了有关如何使用UnityEvent的更多详细信息: http ://docs.unity3d.com/Manual/UnityEvents.html
Alternatively, you should also be able to access the Text using the Text property of the Text control that your InputField is attached to. 或者,您还应该能够使用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 //This is unity Picture where you select method. https://i.stack.imgur.com/CeF1a.png //这是您选择方法的统一图片。 Also worth checking out https://unity3d.com/learn/tutorials/topics/scripting/text-input 还值得一试https://unity3d.com/learn/tutorials/topics/scripting/text-input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.