Get text from Input field in Unity3D with C#

我们两清 提交于 2019-11-27 03:20:56

问题


I'm trying to get a text inside an inputField in Unity3D with C#.

I've placed an inputField in my editor, renamed and tagged in: Username_field.

My question is: How i can get the text inside the InputField Username_field in a C# script?


回答1:


Attach below monobehaviour script to your 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:




回答2:


You can use the "On Value Change" or "End Edit" event of the InputField.

The Unity3D documentation provides more detail on how to use a 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.




回答3:


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. Also worth checking out https://unity3d.com/learn/tutorials/topics/scripting/text-input



来源:https://stackoverflow.com/questions/28273062/get-text-from-input-field-in-unity3d-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!