Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback [Event System] Cannot navigate in UI when FirstSelected is set through code in Update

Discussion in 'UGUI & TextMesh Pro' started by MlleBun, Oct 14, 2019.

  1. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi everyone,

    It's the first time I implement two different menus which require individual FirstSelected gameobjects to be set in the EventSystem component. The logic is : If MenuLeft is opened, set someLeftSlot as FirstSelected. If MenuRight, set someRightSlot as FirstSelected.

    After setting the code, I get the expected behaviour. When opening a given Menu, the correct slot is hightlighted. But then, I can no longer navigate in the displayed menu. When I press on my input to go down, the hightlight is stuck on the FirstSelection slot.

    UI_FirstSelectionBug.gif

    Just to understand the code : I call LeftSlot menu with PS4 L2 and RightSlot menu with PS4 R2. Those menus close when L2 or R2 is released. Once in the given menu, I set the FirstSelected to the first button of that slot in the Update method. I then want to navigate with the DPad vertical buttons.

    Note : When I don't override the EventSystem's FirstSlected, I can acutally navigate in the individual menus, provided I first click on any button with the mouse. Also, I left all the booleans public for this thread's sake.



    Event System setup
    upload_2019-10-14_11-12-13.png


    My script on Canvas
    upload_2019-10-14_11-14-5.png

    The code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class DoubleUINavigationTest : MonoBehaviour
    7. {
    8.     public EventSystem inputEventSystem;
    9.  
    10.     // Left & Right Slots.
    11.     public GameObject leftSlot, rightSlot;
    12.  
    13.     // Left & Right FirstSelection for EventSystem.
    14.     public GameObject leftFirstSelection, rightFirstSelection;
    15.  
    16.     // Left & Right Menus management.
    17.     public bool OneMenuIsOpen; // Only one menu should be opened at a time.
    18.     public bool leftInventory, rightInventory;
    19.     public bool MenuLeft, MenuRight;
    20.  
    21.     // Input L2 & R2.
    22.     private float inputL2, inputR2;
    23.  
    24.     // Input Dpad Vertical.
    25.     private float inputDpad;
    26.  
    27.  
    28.     private void Start()
    29.     {
    30.         // Initialize Menus and inventory togglers.
    31.         OneMenuIsOpen = false;
    32.         leftInventory = false;
    33.         rightInventory = false;
    34.         MenuLeft = false;
    35.         MenuRight = false;
    36.     }
    37.  
    38.  
    39.     private void Update()
    40.     {
    41.         // Toggle between Menu.
    42.         CurrentActiveMenu();
    43.  
    44.         // Set Input
    45.         // leftInventory will be triggered by inputL2.
    46.         // rightInventory will be triggered by inputR2.
    47.         // inputDpad will be used to navigate in the different Menus.
    48.         inputL2 = Input.GetAxis("PS4_L2");
    49.         inputR2 = Input.GetAxis("PS4_R2");
    50.         inputDpad = Input.GetAxis("Dpad_Vertical");
    51.  
    52.         if (inputL2 == 1 && !OneMenuIsOpen)
    53.         {
    54.             OneMenuIsOpen = leftInventory = true;
    55.  
    56.             // Change EventSystem firstSelected.
    57.             ChangeESFirstSelection(leftFirstSelection);
    58.         }
    59.         else OneMenuIsOpen = leftInventory = false;
    60.  
    61.  
    62.         if (inputR2 == 1 && !OneMenuIsOpen)
    63.         {
    64.             OneMenuIsOpen = rightInventory = true;
    65.  
    66.             // Change EventSystem firstSelected.
    67.             ChangeESFirstSelection(rightFirstSelection);
    68.         }
    69.         else OneMenuIsOpen = rightInventory = false;
    70.  
    71.         // Activate/Desactivate Left or Right Slots
    72.         ShowSelectedMenu(MenuLeft, leftSlot);
    73.         ShowSelectedMenu(MenuRight, rightSlot);
    74.     }
    75.  
    76.  
    77.     // Method to Toggle between Menus --------------------------------
    78.     // leftInventory will be triggered by PS4 L2.
    79.     // rightInventory will be triggered by PS4 R2.
    80.     // Note : In other scripts, only one menu will be called at a time
    81.     // with the check bool OneMenuIsOpen.
    82.     // ---------------------------------------------------------------
    83.     private void CurrentActiveMenu()
    84.     {
    85.         if (leftInventory)
    86.         {
    87.             MenuLeft = true;
    88.         }
    89.         else MenuLeft = false;
    90.  
    91.         if (rightInventory)
    92.         {
    93.             MenuRight = true;
    94.         }
    95.         else MenuRight = false;
    96.     }
    97.  
    98.  
    99.     // ----------------------------------------------------------------
    100.     // Method to Activate/Desactivate Left or Right Slots (gameobject)
    101.     // depending on Menu activation condition.
    102.     // ----------------------------------------------------------------
    103.     private void ShowSelectedMenu(bool condition, GameObject selected)
    104.     {
    105.         // If condition is true, setActive(true).
    106.         // Else desactivate the gameobject.
    107.         selected.SetActive(condition);
    108.     }
    109.  
    110.  
    111.     // --------------------------------------------------------------------
    112.     // Method to Change EventSystem firstSelected depending on opened Menu.
    113.     // --------------------------------------------------------------------
    114.     private void ChangeESFirstSelection(GameObject firstSelection)
    115.     {
    116.         inputEventSystem.SetSelectedGameObject(null);   // Hack to fix selection hightlight.
    117.         inputEventSystem.SetSelectedGameObject(firstSelection);
    118.         inputEventSystem.firstSelectedGameObject = firstSelection; // Without this line, the EventSystem FirstSelected field stays empty. Is this needed?
    119.     }
    120. }
    121.  

    // Edit : I saw that when I Debug.Log(inputEventSystem.alreadySelecting), it always returns false.


    When L2 or R2 is pressed
    upload_2019-10-14_11-33-45.png

    Can anyone help me ? Many thanks.
     

    Attached Files:

    Last edited: Oct 14, 2019