- Home /
Touchless GUI Interface - Button Mouseover Emulation
Recently, I've been throwing together a rather dynamic GUI style with buttons and such that scale to the size of the window, etc. I've come to the conclusion that it would be really nice if my community could navigate the menus without requiring the use of their mouse/touchscreen. To do so, I've set up 2D maps for each of the menus, and a rather simplistic navigator. This all seems to work great, except there's currently no way to tell what button I've selected without the debug console. Is there a way to flag a MouseOver event on a button without going through the extensive instructions depicted HERE? My menus are completely renderred in OnGUI() with a variant number of switch-case statements to handle linear branching, ant attaching a script to a GUITexture isn't very applicable in my overall scenario.
Found GUI.FocusControl() and GUI.SetNextControlName(); along with GUI.skin.button = Button; but it seems GUI.FocusControl() won't work outside of OnGUI, so I have to find a way around this set-back.
Answer by Komak57 · Jan 21, 2014 at 01:05 AM
Works wonders for me. Customizable focus color to boot!
Int2D navSel = new Int2D(0,0);
int[][] navMap = new int[][] {new int[] {}};
void Update() {
// Touchless Menu Navigation
if (MainMenu.myInput.Jump.isPressed() || MainMenu.myInput.Fire.isPressed()) {
// select menu
navSel = new Int2D(0,0);
MenuID = navMap[navSel.y][navSel.x];
} else if (MainMenu.myInput.Action.isPressed() || MainMenu.myInput.Melee.isPressed()) {
// back a menu
navSel = new Int2D(0,0);
switch(MenuID) {
case Menu.Settings:
MenuID = Menu.Main;
break;
case Menu.Settings_Graphics:
MenuID = Menu.Settings;
break;
case Menu.Settings_Audio:
MenuID = Menu.Settings;
break;
case Menu.Settings_Controls:
MenuID = Menu.Settings;
break;
default:
ShowMenu = !ShowMenu;
break;
}
} else if (MainMenu.myInput.MoveForward.isPressed()) {
Debug.Log("["+navSel.y+", "+navSel.x+"]:["+navMap.Rank+", "+navMap.Length+"]");
// select menu option above
for(int i = navSel.y; i >= 0; i--) {
Debug.Log("Checking ["+i+", "+navSel.x+"]: "+navMap[i][navSel.x].ToString());
if (navMap[i][navSel.x] != navMap[navSel.y][navSel.x]) {
Debug.Log("Selecting ["+navSel.y+", "+navSel.x+"] Old="+navMap[i][navSel.x].ToString()+", New="+navMap[navSel.y][navSel.x].ToString()+".");
navSel.y = i;
break;
}
}
} else if (MainMenu.myInput.MoveBack.isPressed()) {
Debug.Log("["+navSel.y+", "+navSel.x+"]:["+navMap.Rank+", "+navMap.Length+"]");
// select menu option below
for(int i = navSel.y; i < navMap.Length; i++) {
Debug.Log("Checking ["+(i+1)+", "+(navSel.x+1)+"]: ");//+navMap[i, navSel.x].ToString());
if (navMap[i][navSel.x] != navMap[navSel.y][navSel.x]) {
Debug.Log("Selecting ["+navSel.y+", "+navSel.x+"] Old="+navMap[i][navSel.x].ToString()+", New="+navMap[navSel.y][navSel.x].ToString()+".");
navSel.y = i;
break;
}
}
} else if (MainMenu.myInput.MoveLeft.isPressed()) {
Debug.Log("Size ["+navMap.Rank+", "+navMap.Length+"]");
// select menu option to the left
for(int i = navSel.x; i >= 0; i--) {
if (navMap[navSel.y][i] != navMap[navSel.y][navSel.x]) {
navSel.x = i;
break;
}
}
Debug.Log("Selecting ["+navSel.y+", "+navSel.x+"]: "+navMap[navSel.y][navSel.x].ToString());
} else if (MainMenu.myInput.MoveRight.isPressed()) {
Debug.Log("Size ["+navMap.Rank+", "+navMap.Length+"]");
// select menu option to the right
for(int i = navSel.x; i < navMap[0].Length; i++) {
if (navMap[navSel.y][i] != navMap[navSel.y][navSel.x]) {
navSel.x = i;
break;
}
}
Debug.Log("Selecting ["+navSel.y+", "+navSel.x+"]: "+navMap[navSel.y][navSel.x].ToString());
}
}
void OnGUI() {
GUI.skin.button = Button;
switch(MenuID) {
case Menu.Settings: {
navMap = new int[4][] {
new int[] {Menu.Settings_Graphics},
new int[] {Menu.Settings_Audio},
new int[] {Menu.Settings_Controls},
new int[] {MenuID = Menu.Main},
};
/* Draw buttons and stuff */
break;
}
default: {
break;
}
}
GUI.FocusControl(navMap[navSel.y][navSel.x].ToString());
}