Search Unity

Question I created a custom button and lost the ability for unity to have navigation between custom buttons

Discussion in 'Scripting' started by bzkarcade, Feb 2, 2023.

  1. bzkarcade

    bzkarcade

    Joined:
    Oct 5, 2017
    Posts:
    21
    I created a custom button so that the right click is differentiated from the left

    Code (CSharp):
    1. public class CustomButton : Selectable
    2.     {
    3.         [SerializeField]
    4.         private Button.ButtonClickedEvent m_OnLeftClick = new Button.ButtonClickedEvent();
    5.         [SerializeField]
    6.         private Button.ButtonClickedEvent m_OnRightClick = new Button.ButtonClickedEvent();
    7.  
    8.         public Button.ButtonClickedEvent onLeftClick
    9.         {
    10.             get
    11.             {
    12.                 return this.m_OnLeftClick;
    13.             }
    14.             set
    15.             {
    16.                 this.m_OnLeftClick = value;
    17.             }
    18.         }
    19.  
    20.         public Button.ButtonClickedEvent onRightClick
    21.         {
    22.             get
    23.             {
    24.                 return this.m_OnRightClick;
    25.             }
    26.             set
    27.             {
    28.                 this.m_OnRightClick = value;
    29.             }
    30.         }
    I add these buttons in a menu with a GridLayoutGroup.

    When I try to navigate using keyboard I can't do it


    Screenshot 2023-02-02 131458.png

    As seen in the image, navigation is activated and is recognized by unity. If I try to add classic buttons to this same GridLayoutGroup. These do allow me to navigate

    Screenshot 2023-02-02 132032.png


    I saw in the Button code in unity add these interfaces
    Code (CSharp):
    1. IPointerClickHandler, ISubmitHandler, IEventSystemHandler
    , try to do the test so that it is
    Code (CSharp):
    1. public class CustomButton : Selectable,IPointerClickHandler, ISubmitHandler, IEventSystemHandler
    but it still does not navigate.

    With the mouse everything works perfectly.
     

    Attached Files:

  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    I believe that stuff is controlled by the EventSystem, so Unity doesn't know what the Selected GameObject is until you tell it what it is, as they're custom buttons. Have you tried using EventSystem.SetSelectedGameObject?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Navigation in a Selectable is controlled here:
    Screenshot 2023-02-02 at 3.52.51 PM.png

    Also I don't see a really compelling reason to have created a custom class here. Why not just throw these two components on your object:
    Selectable
    EventTrigger
     
    Kurt-Dekker likes this.
  4. bzkarcade

    bzkarcade

    Joined:
    Oct 5, 2017
    Posts:
    21
    Thanks for your reply


    Navigation is automatic.

    Screenshot 2023-02-02 174831.png

    I did another test overriding the highlighted, pressed and selected colors. But it never turns to the selected color. I think the problem must be linked.


    If I add the Selectable component and Event Trigger, I lose the ability to tell right click from left click. Although the navigation returns
     
    Last edited: Feb 2, 2023
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,837
    I personally wouldn't extend Unity's own component types.

    Make a it a separate component that simply hooks into the existing Unity component.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    bzkarcade likes this.
  7. bzkarcade

    bzkarcade

    Joined:
    Oct 5, 2017
    Posts:
    21