Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

533
Vistas
How to get text input continuously in Unity C#?

I'm going to get text input using TextField and the following is my code:

public Text textInput;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Return)) {
        if (textInput.text == "go") {
            /* do something */
            textInput.text = "";
        }
    }
}

I assigned the Text element under TextField to public Text textInput, and I expected that once a player types "go" and presses enter, the TextField will be cleared and the player be able to type next sentence.

However, after I type "go" and press enter, the text in TextField remained and the focus was out.

How can I clear the TextField maintaining the focus on it?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think the "problem" is that the input field by default also handles the Return key as the submission command which automatically loses the focus.

The default InputField.lineType is

SingleLine

Only allows 1 line to be entered. Has horizontal scrolling and no word wrap. Pressing enter will submit the InputField.

You could instead set it to MultiLineNewLine

Is a multiline InputField with vertical scrolling and overflow. Pressing the return key will insert a new line character.

So it doesn't automatically call the OnSubmit and lose focus when pressing the Return key.


Using that as an alternative to use Update and GetKeyDown you could use InputField.onValidateInput and do something like

public InputField textInput;

private void Awake ()
{
    textInput.lineType = InputField.LineType.MultiLineNewLine;
    textInput.onValidateInput = OnValidate;
}

private char OnValidate(string text, int charIndex, char addedChar)
{
    if(addedChar == '\n')
    {
        if(text.Equals("go"))
        {
            // Do something
        }

        textInput.text = "";
        return '';
    }

    return addedChar;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda