I have a simple login form with email and password input fields next to each other.
Now I set up 2 input axes called UI Horizontal and UI Vertical and set them as navigation axes in the Event System. The UI Vertical includes Down Arrow as well as Tab key.
While the UI navigation seems to work with buttons it doesn’t seem to work with input fields.
When I press Up, Down or Tab on a single-line input field it doesn’t switch the focus.
How could I achieve the expected behavior?
Is this a bug with input fields?
I use this temporary workaround (add on one object, will work for all GUI elements in the scene):
using UnityEngine.UI;
using UnityEngine.EventSystems;
public void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
Selectable next = system.currentSelectedObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next!= null) {
InputField inputfield = next.GetComponent<InputField>();
if (inputfield !=null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
//else Debug.Log("next nagivation element not found");
}
}
If you have clicked on an InputField, it will not only have the control focus, but will be in edit mode. If you press the enter key on your keyboard, that will take that InputField out of edit mode. When not in edit mode, the standard control navigation system (arrow keys) works. So I would not actually consider this a bug. Meaning, it appears to be working as designed.
Having said that, we do need a way to cycle between InputField controls using the tab key. I added a feature request in the feedback section as the built-in bug reporter specifically says if it’s a new feature request, use the feedback system instead of the bug reporter. Will that suffice, or should I be calling this a bug?
Worked perfectly for me. Just had to change some code to some updated functions and variables:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InputNavigator : MonoBehaviour
{
EventSystem system;
void Start()
{
system = EventSystem.current;// EventSystemManager.currentSystem;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
if (inputfield != null)
inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
//else Debug.Log("next nagivation element not found");
}
}
}
I used this and it worked great for ONE set of input fields (I enable and disable them according to the state of the game). When I added this to another script for another set of input fields, it broke for both. Anyone have a clue what I can do to have this in more than one script and have it still work? Only one of the scripts will be enabled at a time.
Disable the interactable setting on each input field you’re not using (a pain) or use a canvas group to disable them entirely when not in use. Just use one tab manager script. Set each input’s navigation as you desire. Use the graphical view for better reference, so you can keep closed groups of navigation sets.
if you also want to make the tab cycle between elements and go back by pressing shift, replace:
Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
by
Selectable next = null;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
if (next==null)
next = system.lastSelectedGameObject.GetComponent<Selectable>();
}
else
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next==null)
next = system.firstSelectedGameObject.GetComponent<Selectable>();
}
I’m actually getting a strange behavior when using it with the Bolt networking debug console overlay on of it. As i toggle through the input controls the debug console disappears then appears.
I think those particular input field bugs with text from one input field showing up in another have recently been fixed in a patch. 4.6.6p3 maybe? Not sure about the Unity 5 though.
The text that disappears I believe is placeholder text. At first I thought it was something to do with the fullscreen mode contracting to fit the software buttons, and causing the text to truncate, but I’ve set it to best fit and it still happens so I’m not sure.
The copied text issue is intermittent and I haven’t been able to reliably reproduce but seems to be actually putting the text into all fields, bypassing validation. Which is a real problem if that’s true.Edit I don’t think it actually bypassing validation. Just a display issue.