- Home /
Question by
ZaxOnTheBeach · Jun 02, 2013 at 09:52 PM ·
guitogglegamepad
Navigating GUI with gamepad on GUI.Toggle
I've been trying to add to this script provided by Farfarer in 2011 so that I can navigate a GUI menu with a gamepad and toggle a GUI.Toggle button on and off with a gamepad BUTTON vs. the left mouse-button click.
An edit to this script done by chelnok is closest to what I want because it allows me to execute a GUI button with the gamepad but I can't get it to work with a toggle.
Here is the script;
// Array of menu item control names.
var menuOptions = new String[4];
menuOptions[0] = "Tutorial";
menuOptions[1] = "Play";
menuOptions[2] = "High Scores";
menuOptions[3] = "Exit";
// Default selected menu item (in this case, Tutorial).
var selectedIndex = 0;
// Function to scroll through possible menu items array, looping back to start/end depending on direction of movement.
function menuSelection (menuItems, selectedItem, direction) {
if (direction == "up") {
if (selectedItem == 0) {
selectedItem = menuItems.length - 1;
} else {
selectedItem -= 1;
}
}
if (direction == "down") {
if (selectedItem == menuItems.length - 1) {
selectedItem = 0;
} else {
selectedItem += 1;
}
}
return selectedItem;
}
function Update ()
{
if (Input.GetKeyDown("down")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "down");
}
if (Input.GetKeyDown("up")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "up");
}
if (Input.GetKeyDown("space")) {
handleSelection();
}
}
function handleSelection()
{
GUI.FocusControl (menuOptions[selectedIndex]);
switch (selectedIndex)
{
case 0:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 1:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 2:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 3:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
default:
Debug.Log ("None of the above selected..");
break;
}
}
function OnGUI ()
{
GUI.SetNextControlName ("Tutorial");
if (GUI.Button(Rect(10,70,170,30), "Button 1 (tutorial,id:0)")) {
selectedIndex = 0;
handleSelection();
}
GUI.SetNextControlName ("Play");
if (GUI.Button(Rect(10,100,170,30), "Button 2 (play, id:1)")) {
selectedIndex = 1;
handleSelection();
}
GUI.SetNextControlName ("High Scores");
if (GUI.Button(Rect(10,130,170,30), "Button 3 (high scores, id:2)")) {
selectedIndex = 2;
handleSelection();
}
GUI.SetNextControlName ("Exit");
if (GUI.Button(Rect(10,160,170,30), "Button 4 (exit, id:3)")) {
selectedIndex = 3;
handleSelection();
}
GUI.FocusControl (menuOptions[selectedIndex]);
}
Any help with this would be very much appreciated!! :)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613103414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Whats wrong with my GUI.Toggle? 2 Answers
Is there a multiple selection ugui toggle group? 1 Answer
Control script generated buttons with a Gamepad 0 Answers
Creating a menu for gamepad? 1 Answer
GUI toggle on click, unless on GUI. 1 Answer