Search Unity

pressing a GUI.Button with a gamepad

Discussion in 'Immediate Mode GUI (IMGUI)' started by moka, Feb 2, 2012.

  1. moka

    moka

    Joined:
    Feb 2, 2012
    Posts:
    2
    Hi all,
    I'm actually building my game menu interface, and I'm trying to make it work with a gamepad type XBOX 360.
    I've found already how to switch the focussed GUI.Button by moving the analogic stick, but I'm stuck when I want to hit the button with the A button of the controller.
    Any ideas, cause I haven't find any way to validate a GUI.Button in script.

    thanks in advance

    MOKA
     
  2. MoyaAdam

    MoyaAdam

    Joined:
    Jul 24, 2010
    Posts:
    50
    Hello, I quickly made a script that uses up and down arrows on a keyboard as well as enter to select a button. This should be easily adaptable to work with a game pad. I used 2 different skins, 1 where it was all the default settings, and the other where the button Hover was set to have green text, when you hit enter it changes the color of that button to it's "Clicked" mode (mind you this is just an altered version of hover) and activated the clickedbutton script. Just use an if command for each different possible button in the clickedbutton function and you should be able to use the button :cool:



    Code (csharp):
    1. var ButtonNames:String[];
    2.  
    3. var NormalSkin:GUISkin;
    4. var ClickedSkin:GUISkin;
    5.  
    6. private var Submited:boolean;
    7. private var ActiveButton:boolean[];
    8. private var MyCount:int=0;
    9. function Start(){
    10.     ActiveButton= new boolean[ButtonNames.Length];
    11. }
    12. function OnGUI () {
    13.  
    14. GUI.FocusControl(ButtonNames[MyCount]);
    15. for(var i:int; i<ButtonNames.Length; i++){
    16.     GUI.SetNextControlName(ButtonNames[i]);
    17.     if(ActiveButton[i])
    18.         GUI.skin=ClickedSkin;
    19.     else
    20.         GUI.skin=NormalSkin;
    21.        
    22.     GUI.Button(Rect(Screen.width/2-50,10+i*30,100,20),ButtonNames[i]);
    23.  
    24. }
    25.  
    26.  
    27.  
    28.  
    29.  
    30. }
    31.  
    32. function Update(){
    33.  
    34. if(Input.GetKeyDown(KeyCode.Return)){
    35.     ActiveButton[MyCount]=true;
    36.     Submited=true;
    37.     ClickedButton(MyCount);
    38. }
    39. if(Input.GetKeyUp(KeyCode.UpArrow)!Submited){
    40.     MyCount-=1;
    41.     if(MyCount<0){
    42.         MyCount=ButtonNames.Length-1;
    43.        
    44.     }
    45. }
    46.  
    47.  
    48. if(Input.GetKeyUp(KeyCode.DownArrow)!Submited){
    49.     MyCount+=1;
    50.     if(MyCount>ButtonNames.Length-1)
    51.         MyCount=0;
    52.        
    53. }
    54. }
    55.  
    56. function ClickedButton(num:int){
    57.     print("Clicked button "+num);
    58.     yield WaitForSeconds(2);
    59.     ActiveButton[MyCount]=false;
    60.     Submited=false;
    61. }
     
  3. moka

    moka

    Joined:
    Feb 2, 2012
    Posts:
    2
    Hey!
    thank you very much for your answer. For what I can see on your code, you are byPassing the GUI.Button and this code could work for a texture as well as for a GUI.Button. What I was really curious about is to validate the button in script in order to execute the same code as if the button was clicked with the mouse.

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     GUI.SetNextControlName("Button1");
    5.     if(GUI.Button(Rect(Screen.width/2-50,10+i*30,100,20),"button1"))
    6.     {
    7.          //Code here
    8.     }
    9. }
    10.  

    and something like this inside the update

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if(Input.GetKeyDown(KeyCode.Return))
    5.     {
    6.          PressButton("Button1");
    7.     }
    8. }
    9.  
    But as far as I can see a method like PressButton taking as parameter a GUI Control name does not exist...