Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

534
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda