I am trying to use a gamepad with a scrollable list of GUI.Buttons and I am looking for a way to attivate a button in the list without having to use the mouse and clicking on the buttons. Any ideas? I’d be happy to clarify something if anyone needs more information.
2 Answers
2Have the button trigger a function when clicked then you can call that function from elsewhere in the code.
function OnGUI(){
if (GUI.Button(Rect(10,10,50,50),"button 1")){
ButtonClicked(0);
}
if (GUI.Button(Rect(10,10,50,50),"button 2")){
ButtonClicked(1);
}
}
function ButtonClicked(id:int){
if(id==0){
//do button 1 stuff
}
if(id==1){
//do button 2 stuff
}
}
I am trying to activate the button without clicking on it with the mouse. So if there was a way of mapping cursor movement to a joystick on the gamepad and then using a trigger to simulate a click that would work well for me.
– mdagredaYou can have the GUI.Button call a new function when clicked. Then its very easy to call this from other locations as well.
function Update(){
if(Input.GetKeyDown(KeyCode.Alpha1)){ //the user presses "1" on their keyboard
DoMyThing();
}
function OnGUI(){
if(GUI.Button(Rect(10,10,100,100),"Click Me Or Press 1")){
DoMyThing();
}
}
function DoMyThing(){
//Do stuff
}
This could even be called from a different script altogether if you make it a static or a public function.
If you need to call the result every frame, set a boolean to true and have it run in update().
– RaydaqI am trying to activate the button without clicking on it with the mouse. So if there was a way of mapping cursor movement to a joystick on the gamepad and then using a trigger to simulate a click that would work well for me.
– mdagredaSorry i guess it was not clear, but this way the actual event that happens after pressing the button is in a different function. I'll edit the answer and put in a another way of triggering it
– Raydaq
Please up vote or accept answers when they are complete.
– RaydaqDid you find a solution for this?
– Moix