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.
Robble
2
Have 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
}
}
Raydaq
3
You 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.