Search Unity

How to set a button as First Selected in the Event System via script ?

Discussion in 'Scripting' started by Mycedra, Apr 18, 2015.

  1. Mycedra

    Mycedra

    Joined:
    Apr 17, 2015
    Posts:
    5
    Good evening everyone. I'm new on this forum and I'm stuck with something that bother me.

    I'm making a videogame as a project for highschool and I'm having a little issue that you can maybe answer.
    I've made a completly scripted main menu (which means that I don't have a canvas in the scene) and have like 5 buttons in. I want to let the user uses the keyboard to navigate through the menu. I know that I've to have an EventSystem in the scene (which I obviously have) and set a button as "First Selected". The problem that I'm facing is that in my Hierarchy, I don't have any buttons.

    All the buttons that I'm displaying on screen are made with code (see below) :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class MainMenu : MonoBehaviour {
    7.     public GUISkin guiSkin;
    8.     public GameObject _firstButtonSelected;
    9.  
    10.     private bool _isFirstMenu=true;
    11.     private bool _isLevelSelectMenu=false;
    12.     private bool _isLoadGameMenu = false;
    13.     private bool _isOptionsMenu = false;
    14.     private bool _isAudioOptions = false;
    15.     private bool _isGraphicsOptions = false;
    16.  
    17.     public string gameTitle;
    18.  
    19.     public float _gameVolume = 0.6f;
    20.     private float _gameFOV = 60.0f;
    21.    
    22.  
    23.     public void Start(){
    24.         //PlayerPrefs.DeleteAll();
    25.  
    26.         _gameVolume = PlayerPrefs.GetFloat ("Game Volume", _gameVolume);
    27.  
    28.         if (PlayerPrefs.HasKey ("Game Volume")) {
    29.             AudioListener.volume = PlayerPrefs.GetFloat ("Game Volume");
    30.         } else {
    31.             PlayerPrefs.SetFloat ("Game Volume", _gameVolume);
    32.         }
    33.     }
    34.  
    35.     public void Update(){
    36.  
    37.     }
    38.  
    39.     public void OnGUI(){
    40.         GUI.skin = guiSkin;
    41.  
    42.         GUI.Label (new Rect (25, 75, 300, 50), gameTitle, "Menu Title");
    43.  
    44.         FirstMenu ();
    45.         LoadGameMenu ();
    46.         LevelSelectMenu ();
    47.         OptionsMenu ();
    48.         AudioOptionsDisplay ();
    49.         GraphicsOptionsDisplay ();
    50.  
    51.         if (_isLoadGameMenu == true || _isOptionsMenu == true || _isLevelSelectMenu == true) {
    52.             if (GUI.Button (new Rect (25, Screen.height - 60, 200, 50), "back", "Button Style")) {
    53.                 _isLevelSelectMenu = false;
    54.                 _isLoadGameMenu=false;
    55.                 _isOptionsMenu=false;
    56.                 _isAudioOptions=false;
    57.                 _isGraphicsOptions=false;
    58.  
    59.                 _isFirstMenu=true;
    60.             }
    61.         }
    62.     }
    63.  
    64.     public void FirstMenu(){
    65.         if (_isFirstMenu) {
    66.  
    67.  
    68.             if (GUI.Button(new Rect(25, Screen.height / 2 - 100, 200, 50), "new game", "Button Style"))
    69.             {
    70.                 Application.LoadLevel ("level1");
    71.             }
    72.  
    73.             if (GUI.Button(new Rect(25, Screen.height / 2 - 40, 200, 50), "load game", "Button Style"))
    74.             {
    75.                 _isFirstMenu=false;
    76.                 _isLoadGameMenu=true;
    77.             }
    78.  
    79.             if (GUI.Button(new Rect(25, Screen.height / 2 + 20, 200, 50), "level select", "Button Style"))
    80.             {
    81.                 _isFirstMenu=false;
    82.                 _isLevelSelectMenu=true;
    83.             }
    84.  
    85.             if (GUI.Button(new Rect(25, Screen.height / 2 + 80, 200, 50), "options", "Button Style"))
    86.             {
    87.                 _isFirstMenu=false;
    88.                 _isOptionsMenu=true;
    89.             }
    90.  
    91.             if (GUI.Button(new Rect(25, Screen.height / 2 + 140, 200, 50), "quit", "Button Style"))
    92.             {
    93.                 Application.Quit();
    94.             }
    95.         }
    96.     }
    97.     public void LoadGameMenu(){
    98.         if (_isLoadGameMenu) {
    99.  
    100.         }
    101.  
    102.     }
    103.     public void LevelSelectMenu(){
    104.         if (_isLevelSelectMenu){
    105.             GUI.Label(new Rect(25, Screen.height / 2 - 200, 400, 50), "level select", "Sub Menu Title");
    106.             if (GUI.Button(new Rect(25, Screen.height / 2 - 100, 200, 100), "Level 01", "Button Style"))
    107.             {
    108.  
    109.             }
    110.             if (GUI.Button(new Rect(235, Screen.height / 2 - 100, 200, 100), "Level 02", "Button Style"))
    111.             {
    112.  
    113.             }
    114.             if (GUI.Button(new Rect(445, Screen.height / 2 - 100, 200, 100), "Level 03", "Button Style"))
    115.             {
    116.  
    117.             }
    118.             if (GUI.Button(new Rect(655, Screen.height / 2 - 100, 200, 100), "Level 04", "Button Style"))
    119.             {
    120.  
    121.             }
    122.         }
    123.     }
    124.     public void OptionsMenu(){
    125.         if (_isOptionsMenu) {
    126.             GUI.Label (new Rect(25, Screen.height/2-200, 200, 50), "options", "Sub Menu Title");
    127.  
    128.             //GUI.Box (new Rect(10, Screen.height/2-100, Screen.width/2-150, Screen.height-450), "");
    129.             if(_isAudioOptions==true || _isGraphicsOptions==true){
    130.             GUI.Box (new Rect(Screen.width/2, 0, Screen.width/2, Screen.height),"", "Box Style");
    131.             }
    132.             if (GUI.Button(new Rect(25, Screen.height / 2 + 20, 200, 50), "audio options", "Button Style"))
    133.             {
    134.                 _isGraphicsOptions=false;
    135.                 _isAudioOptions=true;
    136.             }
    137.  
    138.             if (GUI.Button(new Rect(25, Screen.height / 2 + 80, 200, 50), "graphics options", "Button Style"))
    139.             {
    140.                 _isGraphicsOptions=true;
    141.                 _isAudioOptions=false;
    142.             }
    143.         }
    144.     }
    145.     public void AudioOptionsDisplay(){
    146.         if (_isAudioOptions) {
    147.             GUI.Label(new Rect(Screen.width/2+25, 10, 400, 50), "audio settings", "Sub Menu Title");
    148.             GUI.Label (new Rect(Screen.width/2 +25,100,200,25),"game volume");
    149.             _gameVolume=GUI.HorizontalSlider(new Rect(Screen.width/2 + 10, 150, Screen.width/2-20, 25), _gameVolume, 0.0f, 1.0f);
    150.             //GUI.Label (new Rect(Screen.width - 35, 145 , 50,25), "" + (System.Math.Round(_gameVolume, 2)));
    151.             AudioListener.volume=_gameVolume;
    152.  
    153.             if (GUI.Button(new Rect(Screen.width / 2 + 10, Screen.height - 60 , 150, 50), "apply", "Button Style"))
    154.             {
    155.                 PlayerPrefs.SetFloat("Game Volume", _gameVolume);
    156.             }
    157.         }
    158.     }
    159.     public void GraphicsOptionsDisplay(){
    160.         if (_isGraphicsOptions) {
    161.  
    162.             GUI.Label(new Rect(Screen.width / 2 + 25, 10, 400, 50), "video settings", "Sub Menu Title");
    163.             GUILayout.BeginVertical ();
    164.  
    165.             GUI.Label (new Rect (Screen.width / 2 + 25, 200, 400, 25), "graphics quality");
    166.             for (int i=0; i<QualitySettings.names.Length; i++) {
    167.                 if (GUI.Button(new Rect(Screen.width / 2 + 30, 235 + i * 60, 150, 50), QualitySettings.names[i], "Button Style"))
    168.                     QualitySettings.SetQualityLevel (i, true);
    169.             }
    170.             GUILayout.EndVertical();
    171.             if (GUI.Button(new Rect(Screen.width / 2 + 10, Screen.height - 60, 150, 50), "apply", "Button Style"))
    172.                 PlayerPrefs.SetFloat("game FOV", _gameFOV);
    173.         }
    174.     }
    175. }
    Can I set a button as "First Selected" event system in the code.



    Thanks for your answers and forgive my bad English, I'm French. ;)

    (PS : I've made the code to let the buttons scale with the window, maybe I've done it wrong).
     

    Attached Files:

    Last edited: Apr 18, 2015
  2. Mycedra

    Mycedra

    Joined:
    Apr 17, 2015
    Posts:
    5
    Bump, can somebody help me please ? That's the only thing I got left to finish my project.

    Help please.
     
  3. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    (Post edited) A more complete example:
    Code (CSharp):
    1. var eventSystem = EventSystemManager.currentSystem;
    2. eventSystem.SetSelectedGameObject( gameObject, new BaseEventData(eventSystem));
    where gameObject is a reference to the the button you want to be selected.

    But I'm not sure if the EventSystem can even be used with the legacy GUI stuff, as you don't create any specific references to buttons, just define them in OnGUI? Someone else will have to clarify that, I fear.
     
    Last edited: Apr 18, 2015
    Lefko and _Prism_ like this.
  4. Mycedra

    Mycedra

    Joined:
    Apr 17, 2015
    Posts:
    5
    Hi Xoduz, and thanks for your answer. I've already try that command but I don't know where to put it. Any suggestions ?
     
  5. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    Updated my first reply, which was incomplete.
     
    _Prism_ likes this.
  6. Mycedra

    Mycedra

    Joined:
    Apr 17, 2015
    Posts:
    5
    I tried your example, but sadly, it doesn't work. :C

    Can anyone confirm if there is a way to make it through the legacy or do I have to use a canvas (which I really don't want to ... :'c)
     
  7. Mycedra

    Mycedra

    Joined:
    Apr 17, 2015
    Posts:
    5
    bump, anyone have an answer ?
     
  8. _Prism_

    _Prism_

    Joined:
    Jan 25, 2014
    Posts:
    11
    I know this is a suuuuper old thread but I just wanted to say thanks. This solved my issue perfectly.
    I would add that you need to include the UnityEngine.EventSystems namespace to ensure it works =)
    Code (csharp):
    1. using UnityEngine.EventSystems;
    2.  
    3.     //set this in the Inspector
    4.     [SerializeField]
    5.     private GameObject itemsButton;
    6.  
    7.     public void OpenCloseMenu()
    8.     {
    9.         //toggle menu window open/closed
    10.         mainMenu.SetActive(!mainMenu.activeSelf);
    11.         //set Items button as first selected
    12.         var eventSystem = EventSystem.current;
    13.         eventSystem.SetSelectedGameObject(itemsButton, new BaseEventData(eventSystem));
    14.         //update menu data
    15.         UpdateMainStats();
    16.     }
     
  9. Naakoo_

    Naakoo_

    Joined:
    Nov 5, 2017
    Posts:
    1
    I didn't understand, where did you add this code ? Thanks.
     
  10. OddBawlStudios

    OddBawlStudios

    Joined:
    Jul 18, 2019
    Posts:
    1
    The "using UnityEngine.EventSystems;" is added at the top of the scripts to access them.