Search Unity

Unity UI Possible to always have an element selected?

Discussion in 'UGUI & TextMesh Pro' started by Carwashh, Oct 18, 2018.

  1. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    763
    I've got a UI screen setup with 4 buttons, when one of them becomes the selectable some other element is displayed, one of them is set as selected by default.

    Now, if you click anywhere on the screen (off these buttons) then the current selectable is DeSelected. Is there a way to stop this behaviour, and always keep something selected?
     
  2. FerdieQO

    FerdieQO

    Joined:
    Apr 21, 2013
    Posts:
    5
    I'm aware that this question is asked a couple of years ago, but I stumbled onto the same issue and found a simple fix. Perhaps someone else reading this will benefit from this. My solution is a simple MonoBehaviour that I add to the EventSystem GameObject.

    I recommend to go to the EventSystem component and set up the `First Selected` GameObject, so that the game always starts with a UI element selected (if you'd want that).

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. namespace UIExample.Code.UI
    7. {
    8.     public class ForceUIElementToBeSelectedComponent : MonoBehaviour
    9.     {
    10.         private EventSystem currentEventSystem;
    11.         private GameObject currentlySelected;
    12.  
    13.         private void Awake()
    14.         {
    15.             DontDestroyOnLoad(gameObject);
    16.         }
    17.  
    18.         private void Start()
    19.         {
    20.             currentEventSystem = EventSystem.current;
    21.             currentlySelected = currentEventSystem.currentSelectedGameObject;
    22.         }
    23.  
    24.         private void Update()
    25.         {
    26.             //Check if the last known selected GameObject has changed since
    27.             //the last frame
    28.             if(currentEventSystem.currentSelectedGameObject != null &&
    29.                 currentlySelected != currentEventSystem.currentSelectedGameObject)
    30.             {
    31.                 currentlySelected = currentEventSystem.currentSelectedGameObject;
    32.             }
    33.  
    34.             // The currentSelectedGameObject will be null when you click with your
    35.             // anywhere on the screen on a non-Selectable GameObject.
    36.             if(currentEventSystem.currentSelectedGameObject == null)
    37.             {
    38.                 // If this happens simply re-select the last known selected GameObject.
    39.                 if (currentlySelected != null)
    40.                 {
    41.                     currentlySelected.GetComponent<Selectable>().Select();
    42.                 }
    43.                 else
    44.                 {
    45.                     // If there is none, select the firstSelectedGameObject
    46.                     // (which can be setup inthe EventSystem component).
    47.                     currentlySelected = currentEventSystem.firstSelectedGameObject;
    48.                     currentlySelected.GetComponent<Selectable>().Select();
    49.                 }
    50.             }
    51.         }
    52.     }
    53. }
    54.