I can’t figure out how to use the new input field. there doesn’t seem any way to transfer it to a string in a script. Does anyone know how I could do that?
Here’s code for both catching the submission (this happens when you press a button that is set as Submit in the input system - Enter by default) and using a button to submit:
(include UnityEngine.UI for access to UI components)
[SerializeField]
private InputField nameInputField = null;
[SerializeField]
private Button submitButton = null;
private void Start()
{
// Add listener to catch the submit (when set Submit button is pressed)
nameInputField.onSubmit.AddListener((value) => SubmitName(value));
// Add validation
nameInputField.validation = InputField.Validation.Alphanumeric;
// This is a setup for a button that grabs the field value when pressed
submitButton.onClick.AddListener(() => SubmitName(nameInputField.value));
}
private void SubmitName(string name)
{
//What to do with the value from input field
}
Hmmm… unity is annoying me… with the various UI changes, and the removal of onSubmit… I had to put a check in Update.
void Update(){
if (chatInput.isFocused) {
if(Input.GetButtonDown("Submit")){
.. do things ..
}
}
}