Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Tab Between Input Fields & Button

Discussion in 'Scripting' started by mholmes, Oct 13, 2018.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    101
    Working on a create account scene and I'm trying to setup navigation. I'm getting an error system object null. Aka it can't find my object. Not sure what I've done wrong:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class InputNavigator : MonoBehaviour
    7. {
    8.     public InputField _tbEmail;
    9.     public InputField _tbPassword;
    10.     public InputField _tbBirthMM;
    11.     public InputField _tbBirthDD;
    12.     public InputField _tbBirthYYYY;
    13.     public InputField _tbChatHandle;
    14.     public InputField _tbFirstName;
    15.     public InputField _tbLastName;
    16.     public InputField _tbAddress;
    17.     public InputField _tbCity;
    18.     public InputField _tbState;
    19.     public InputField _tbZip;
    20.     public InputField _tbRecoveryQuestion;
    21.     public InputField _tbRecoveryAnswer;
    22.     public Button _btnCreateAccount;
    23.  
    24.     private EventSystem myEventSystem;
    25.  
    26.     void Start()
    27.     {
    28.         myEventSystem = EventSystem.current;// EventSystemManager.currentSystem;
    29.  
    30.     }
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.Tab))
    35.         {
    36.             string current = myEventSystem.currentSelectedGameObject.GetComponent<Selectable>().name;
    37.             myEventSystem = GetComponent<EventSystem>();
    38.  
    39.             switch (current)
    40.             {
    41.                 case "tbEmail":
    42.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbPassword"), new BaseEventData(myEventSystem));
    43.                     break;
    44.                 case "tbPassword":
    45.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbBirthDate_MM"), new BaseEventData(myEventSystem));
    46.                     break;
    47.                 case "tbBirthDate_MM":
    48.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbBirthDate_DD"), new BaseEventData(myEventSystem));
    49.                     break;
    50.                 case "tbBirthDate_DD":
    51.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbBirthDate_YYYY"), new BaseEventData(myEventSystem));
    52.                     break;
    53.                 case "tbBirthDate_YYYY":
    54.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbChatHandle"), new BaseEventData(myEventSystem));
    55.                     break;
    56.                 case "tbChatHandle":
    57.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbFirstName"), new BaseEventData(myEventSystem));
    58.                     break;
    59.                 case "tbFirstName":
    60.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbLastName"), new BaseEventData(myEventSystem));
    61.                     break;
    62.                 case "tbLastName":
    63.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbAddress"), new BaseEventData(myEventSystem));
    64.                     break;
    65.                 case "tbAddress":
    66.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbCity"), new BaseEventData(myEventSystem));
    67.                     break;
    68.                 case "tbCity":
    69.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbState"), new BaseEventData(myEventSystem));
    70.                     break;
    71.                 case "tbState":
    72.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbZipCode"), new BaseEventData(myEventSystem));
    73.                     break;
    74.                 case "tbZipCode":
    75.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbRecoveryQuestion"), new BaseEventData(myEventSystem));
    76.                     break;
    77.                 case "tbRecoveryQuestion":
    78.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbRecoveryAnswer"), new BaseEventData(myEventSystem));
    79.                     break;
    80.                 case "tbRecoveryAnswer":
    81.                     myEventSystem.SetSelectedGameObject(GameObject.Find("btnCreateAccount"), new BaseEventData(myEventSystem));
    82.                     break;
    83.                 case "btnCreateAccount":
    84.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbEmail"), new BaseEventData(myEventSystem));
    85.                     break;
    86.                 default:
    87.                     myEventSystem.SetSelectedGameObject(GameObject.Find("tbEmail"), new BaseEventData(myEventSystem));
    88.                     break;
    89.             }
    90.         }
    91.     }
    92. }
    93.  
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    53
    That's allot of code for me to see whats happening without testing, although if I may add a suggestion to your current tabbing logic.

    When writing desktop applications, tabbing is controlled by each object having a bool tabStop and tabIndex.
    So tabStop says if that object is tabbable, and tabIndex says what order it is in.

    So I would recommend an implementation of something like this.. Its hacky but I threw it together real quick.
    Just drag your list of objects to the list in order you want the to cycle, and set there tabstop to true.

    Code (CSharp):
    1. public class TabNavigator : MonoBehaviour
    2. {
    3.     public int startIndex = 0;
    4.     private int targetIndex;
    5.    
    6.     public List<ObjectTab> objectTabs = new ist<ObjectTab>();
    7.     private EventSystem myEventSystem;
    8.    
    9.     class ObjectTab
    10.     {
    11.         public bool tabStop;
    12.         public GameObject tabObject;
    13.     }
    14.    
    15.     void Start()
    16.     {
    17.         myEventSystem = EventSystem.current;
    18.         targetIndex = startIndex - 1;
    19.         SetCurrentTabObject(targetIndex);
    20.     }
    21.    
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.Tab))
    25.             SetCurrentTabObject();
    26.     }
    27.    
    28.     void SetCurrentTabObject()
    29.     {
    30.         targetIndex ++;
    31.         if (targetIndex > objectTabs.Count)
    32.             targetIndex = 0;
    33.         if (!objectTabs[targetIndex].tabStop || !objectTabs[targetIndex].tabObject.activeSelf)
    34.         {
    35.             SetCurrentTabObject()
    36.             return;
    37.         }
    38.         myEventSystem.SetSelectedGameObject(objectTabs[targetIndex].tabObject);  
    39.     }
    40. }
    Alternatively, add a tabIndex to the ObjectTab class and do a search to find the the index number or next highest. That way you don't have to worry about the order in which they are in the list. But I'll leave that up to you :)