How to get the text form input text in unity?
How to get the text form input text in unity?
To get the text from an input field in Unity, you can use the
text
property of the InputField
component. Here's an example of how you can do it:using UnityEngine;
using UnityEngine.UI;
public class ExampleScript : MonoBehaviour
{
public InputField inputField;
void Start()
{
// Get the text from the input field when the button is clicked
string inputText = inputField.text;
}
}
In this example, inputField
is a public InputField
component that is attached to a GameObject in your scene. When the button is clicked, the script gets the text from the input field and stores it in a string variable called inputText
.
You can also use the onEndEdit
event of the InputField
component to get the text whenever the player finishes editing the input field. Here's an example of how you can do that:
using UnityEngine;
using UnityEngine.UI;
public class ExampleScript : MonoBehaviour
{
public InputField inputField;
void Start()
{
inputField.onEndEdit.AddListener(GetInputText);
}
void GetInputText(string inputText)
{
// Use the input text here
}
}
In this example, the
GetInputText
function is called whenever the player finishes editing the input field. The inputText
parameter of the function contains the text that was entered in the input field.
Post a Comment
image video quote pre code