Hi,
I’m working now in a pause screen where you have the following options:
- Continue Playing
- Adjust Volume → Triggers a new menu screen to adjust the volume
- Exit → Triggers a new menu screen to confirm the selection
First Menu - Moving throught options
There if i move with the keyboard or the joystick the GUIFocusControl is working fine, changing the colors of the buttons when is active or onHover.
The problem comes with the “Adjust Volume Menu”, the keyboard is changing betweeen options, but it doesnt’ change the colors of the button unless i use the mouse onHover:
Volume Menu - No active buttons or bar
Volume Menu - Mouse on Hover on the bar
Volume Menu - Mouse on Hover on the button
Also, this is working just fine in the confirm menu:
The worst part of all, is that i’m using almost the same script in another game and is working just fine. I really don’t know what is happening.
I hope you can help me out here.
I leave the script right here:
#pragma strict
var MainMenu:String;
var skin:GUISkin;
private var savedTimeScale:float;
private var screenWidth:int;
private var screenHeight:int;
var pauseButton:String;
var statColor:Color = Color.yellow;
enum Page {
None,Main,Volume, Confirm
}
private var selectedIndex:int = 0;
private var mainMenu:String[] = ["Continuar", "Volumen", "Salir"];
private var volumeMenu:String[] = ["Volumen", "Volver"];
private var confirmMenu:String[] = ["Confirmar", "Volver"];
var menuClip:AudioClip;
var menuExplanationClip:AudioClip;
var defaultOptionClip:AudioClip;
var volumeClip:AudioClip;
var exitClip:AudioClip;
var volumeExplanationClip:AudioClip;
var backClip:AudioClip;
var continueClip:AudioClip;
var beepClip:AudioClip;
var confirmExplanationClip: AudioClip;
var confirmClip: AudioClip;
var backmainClip: AudioClip;
var playerAudio:PlayAtPlayer;
private var mainMenuAudio:AudioClip[];
private var volumeMenuAudio:AudioClip[];
private var confirmMenuAudio:AudioClip[];
private var currentPage:Page;
var continue_skin : GUISkin;
var adjustVolume_skin : GUISkin;
var volumeMenu_skin : GUISkin;
var volumeBar_skin : GUISkin;
var exit_skin : GUISkin;
var confirm_skin : GUISkin;
var cancel_skin : GUISkin;
function Start() {
playerAudio = GameObject.FindGameObjectWithTag("PlayerAudio").GetComponent(PlayAtPlayer);
mainMenuAudio = [ continueClip, volumeClip, exitClip ];
volumeMenuAudio = [ volumeExplanationClip, backClip ];
confirmMenuAudio = [confirmExplanationClip, confirmClip, backClip ];
screenWidth = Screen.width;
screenHeight = Screen.height;
Time.timeScale = 1.0;
}
function LateUpdate () {
if (Input.GetButtonDown(pauseButton) || XInput.GetButtonDown(pauseButton)) {
switch (currentPage) {
case Page.None: PauseGame(); break;
case Page.Main: UnPauseGame(); break;
default: playerAudio.PlayWithPriority(menuClip, 5); currentPage = Page.Main; break;
}
}
var currentMenu:String[];
switch(currentPage) {
case Page.Main: currentMenu = mainMenu; break;
case Page.Volume: currentMenu = volumeMenu; break;
case Page.Confirm: currentMenu = confirmMenu; break;
default: return;
}
if (Input.GetButtonDown("Down") || XInput.GetButtonDown("Down")) {
menuSelection(currentMenu, "down");
}
if (Input.GetButtonDown("Up") || XInput.GetButtonDown("Up")) {
menuSelection(currentMenu, "up");
}
if (Input.GetButtonDown("Enter") || XInput.GetButtonDown("A")) {
handleSelection("Enter");
}
if (Input.GetButtonDown("Left") || XInput.GetButtonDown("Left")) {
handleSelection("Left");
}
if (Input.GetButtonDown("Right") || XInput.GetButtonDown("Right")) {
handleSelection("Right");
}
}
function menuSelection (currentMenu:Array, direction:String) {
var audioClips:AudioClip[];
switch(currentPage) {
case Page.Main: audioClips = mainMenuAudio; break;
case Page.Volume: audioClips = volumeMenuAudio; break;
case Page.Confirm: audioClips = confirmMenuAudio; break;
default: return;
}
if (direction == "up") {
if (selectedIndex == 0) {
selectedIndex = currentMenu.length - 1;
} else {
selectedIndex -= 1;
}
}
if (direction == "down") {
if (selectedIndex == currentMenu.length - 1) {
selectedIndex = 0;
} else {
selectedIndex += 1;
}
}
playerAudio.PlayWithPriority(audioClips[selectedIndex], 5);
}
function handleSelection(command:String) {
switch(currentPage) {
case Page.Main:
if (command == "Enter") {
if (selectedIndex == 0) {
UnPauseGame();
break;
} else if (selectedIndex == 1) {
selectedIndex = 0;
currentPage = Page.Volume;
playerAudio.PlayWithPriority(volumeExplanationClip, 5);
break;
} else if (selectedIndex == 2) {
selectedIndex = 0;
currentPage = Page.Confirm;
playerAudio.PlayWithPriority(confirmExplanationClip, 5);
break;
}
}
break;
case Page.Volume:
if (selectedIndex == 0) {
if (command == "Left") {
AudioListener.volume = Mathf.Max(0, AudioListener.volume - 0.1);
playerAudio.PlayWithPriority(beepClip, 5);
}
if (command == "Right") {
AudioListener.volume = Mathf.Min(1.0, AudioListener.volume + 0.1);
playerAudio.PlayWithPriority(beepClip, 5);
}
}
if (command == "Enter") {
if (selectedIndex == 0) {
selectedIndex = 1;
playerAudio.PlayWithPriority(beepClip, 5);
} else {
selectedIndex = 0;
currentPage = Page.Main;
playerAudio.PlayWithPriority(menuClip, 5);
}
}
break;
case Page.Confirm:
if (command == "Enter") {
if (selectedIndex == 0) {
UnPauseGame();
Application.LoadLevel(MainMenu);
break;
} else {
selectedIndex = 0;
currentPage = Page.Main;
playerAudio.PlayWithPriority(menuClip, 5);
}
}
break;
default: break;
}
}
function OnGUI () {
if (skin != null) {
GUI.skin = skin;
}
screenWidth = Screen.width;
screenHeight = Screen.height;
if (IsGamePaused()) {
//GUI.color = statColor;
switch (currentPage) {
case Page.Main: PauseMenu(); break;
case Page.Volume: VolumeMenu(); break;
case Page.Confirm: ConfirmationMenu(); break;
}
}
var currentMenu:Array;
switch(currentPage) {
case Page.Main: currentMenu = mainMenu; break;
case Page.Volume: currentMenu = volumeMenu; break;
case Page.Confirm: currentMenu = confirmMenu; break;
default: return;
}
GUI.FocusControl(currentMenu[selectedIndex]);
}
function PauseMenu() {
BeginPage(screenWidth/2,screenHeight/2);
GUI.skin = continue_skin;
GUI.SetNextControlName("Continuar");
if (GUILayout.Button ("")) {
UnPauseGame();
}
GUI.skin = adjustVolume_skin;
GUI.SetNextControlName("Volumen");
if (GUILayout.Button ("")) {
selectedIndex = 0;
currentPage = Page.Volume;
}
GUI.skin = exit_skin;
GUI.SetNextControlName("Salir");
if (GUILayout.Button ("")) {
selectedIndex = 0;
currentPage = Page.Confirm;
}
EndPage();
}
function VolumeMenu() {
BeginPage(screenWidth / 2, screenWidth / 2);
GUI.skin = volumeBar_skin;
GUILayout.Label("Volume");
GUILayout.Space (20);
GUI.SetNextControlName("Volume");
AudioListener.volume = GUILayout.HorizontalSlider(AudioListener.volume,0.0,1.0);
GUILayout.Space (20);
GUI.skin = volumeMenu_skin;
GUI.SetNextControlName("Go Back");
if (GUILayout.Button("")) {
selectedIndex = 0;
currentPage = Page.Main;
}
EndPage();
}
function ConfirmationMenu(){
BeginPage(screenWidth / 2 -400, screenHeight / 2);
GUI.skin = confirm_skin;
GUILayout.Label("Confirm that you want to go back to the Main Screen");
GUILayout.Space(20);
GUI.SetNextControlName("Confirmar");
if (GUILayout.Button ("")) {
Application.LoadLevel(MainMenu);
}
GUI.skin = cancel_skin;
GUI.SetNextControlName("Volver");
if (GUILayout.Button("")) {
selectedIndex = 0;
currentPage = Page.Main;
}
EndPage();
}
function BeginPage(width:int,height:int) {
GUILayout.BeginArea(Rect((Screen.width - width)/2-50,(Screen.height - height)/2,width+200,height));
}
function EndPage() {
GUILayout.EndArea();
}
function PauseGame() {
savedTimeScale = Time.timeScale;
Time.timeScale = 0;
AudioListener.pause = true;
currentPage = Page.Main;
selectedIndex = 0;
var pauseEndTime:float;
pauseEndTime = Time.realtimeSinceStartup + menuClip.length + 0.1;
playerAudio.PlayWithPriority(menuClip, 4);
while (Time.realtimeSinceStartup < pauseEndTime) {
yield 0;
}
if (IsGamePaused()) {
playerAudio.PlayWithPriority(menuExplanationClip, 4);
}
pauseEndTime = Time.realtimeSinceStartup + menuExplanationClip.length + 0.1 ;
while (Time.realtimeSinceStartup < pauseEndTime) {
yield 0;
}
if (IsGamePaused()) {
playerAudio.PlayWithPriority(defaultOptionClip, 4);
}
}
function UnPauseGame() {
playerAudio.audio.Stop();
Time.timeScale = savedTimeScale;
AudioListener.pause = false;
currentPage = Page.None;
}
function IsGamePaused() {
return Time.timeScale==0;
}
function OnApplicationPause(pause:boolean) {
if (IsGamePaused()) {
AudioListener.pause = true;
}
}