Search Unity

Unity UI Why can't I automatically select my input field?

Discussion in 'UGUI & TextMesh Pro' started by Darkforge317, Dec 29, 2018.

  1. Darkforge317

    Darkforge317

    Joined:
    Oct 4, 2017
    Posts:
    2
    I'm currently developing a Highscore system for Android.
    I have an Input field where the player will enter their name.

    I want it so that the player doesn't have to tap on this input field in order to type into it.
    However, all my attempts have failed so far.


    I currently have a prefab called HighScoreTemplate.
    This prefab has a UI Input Field (for the name) and UI Text object (for the score).
    It also has a script on it called HighScoreTemplate.cs

    I created a Select() function in the HighScoreTemplate.cs script that will call .Select() on the input field and then .ActivateInputField() afterwards.


    Code (CSharp):
    1. public void Select()
    2. {
    3.     // Select it
    4.     NameField.Select();
    5.     NameField.ActivateInputField();
    6. }

    However, this wasn't automatically selecting the NameField input field, as the keyboard did not pop up on my Android device.

    I have verified through debugging that this Select() function is being called and run.

    I have also verified that this input field is interactable, I can see the checkbox for it checked in the inspector when I select it.

    Other Things I've Tried
    I then assumed that maybe Unity had a delay, so I attempted this test to see if it would work:
    Code (CSharp):
    1. int counter = 1;
    2. while(counter <= 50 && !_newHighScoreTemplate.NameField.isFocused)
    3. {
    4.     _newHighScoreTemplate.Select();
    5.     Debug.Log("Select Attempt #" + counter);
    6.     counter++;
    7. }

    This test gave me the same result, the android keyboard did not pop up and allow me to input text into the input field.


    I have also attempted every solution found in this thread
    Including using
    Code (CSharp):
    1. EventSystem.current.SetSelectedGameObject(NameField.gameObject);



    Can someone please shed some light on what I'm doing wrong?
    Why is my input field not gaining focus and why is the keyboard in Android not popping up so that I can type into the field without having to tap onto it first?
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Are you trying to do this via a press (or some other event) somewhere else? If so, that event could interfere with the selection of the input field. You might need to wait a frame instead in this case. Try starting this coroutine instead.
    Code (CSharp):
    1. public IEnumerator WaitForInputActivation()
    2. {
    3.     yield return 0;
    4.     NameField.ActivateInputField();
    5. }
     
    shaneK001 and Darkforge317 like this.
  3. Darkforge317

    Darkforge317

    Joined:
    Oct 4, 2017
    Posts:
    2
    I'm calling this immediately after a scene loads.
    The scene basically checks if there was a new highscore achieved, and if so then it shows the Highscore table and selects the input field.

    As for your solution.
    It works!

    Except I wrote this co-routine in a different script so I had to make one modification, I also made it to
    yield return null
    instead so that memory wouldn't be allocated.

    I wrote it like this:
    Code (CSharp):
    1. private IEnumerator SelectNewHighScoreTemplate()
    2. {
    3.     if (_newHighScoreTemplate != null)
    4.     {
    5.         yield return null;
    6.         _newHighScoreTemplate.Select();
    7.     }
    8. }
    It turns out that I was instantiating the prefab that contained the input field on the same frame that I was attempting to select the input field.
    So the game object wasn't "active" until one frame later.

    I feel as though I should have thought of this...

    Regardless,
    Thank you very much for your solution!
     
    shaneK001 and bentontr like this.