Block Escape key while using SubMenu
I have a pause menu that gets activated when pressing Escape. In that menu I have a series of buttons that when clicking on, close the previous menu and open a new panel with the option of going backwards. The thing is that if I press Escape once inside of the second panel, the initial pause menu overlays. My intention is to block the key Escape when the second menu is on, so it doesn't happen but I don't know how :/
Any clues/method I could try?
Thank you
Answer by Darkforge317 · May 14, 2018 at 10:48 PM
Can you please show the code? It would help a lot.
Here's my advice without seeing the code though... Create an enum and call it PauseState. This enum should have as many states as there are panels... So if you have the MainPause panel and a settings panel, then make sure the enum has "Main" and "Settings" as possible states.
public enum PauseState
{
Main,
Settings
}
PauseState myState = PauseState.Main;
Now, whenever you click on the button for settings, simply set myState to PauseState.Settings.
public void SettingsButton()
{
// Turn off the main panel
mainPanel.SetActive(false):
// Turn on the settings panel
settingsPanel.SetActive(true):
// We're now in the settings menu
myState = PauseState.Settings;
}
That way, when you press Escape, you can check if myState is Main or not.
// If we press Escape
if (Input.GetKeyDown(KeyCode.Escape))
{
// If we're currently paused
if(isPaused)
{
// If we're on the main panel
if(myState == PauseState.Main)
{
// Unpause the game
Unpause();
}
}
// If we're not currently paused
else
{
// Pause the game
Pause();
}
}
Answer by ArnauRapid · May 15, 2018 at 12:50 AM
Thank you for your answer @Darkforge317 :D, I understand that my problem can be a little bit confusing, that's why I did these gifs to try to show better what is the issue I am facing. Here you can see how I access the game and when pressing Escape and accessing the audiosettings the pause menu overlays the audio settings menu. This is what I am trying to avoid.
As you can see in my Hierarchy I have different panels and I change between them by using the OnClick() options setting active or unactive their visibility. To fix the problem with the Escape I was trying to play with the interactability and alpha map of the PausePanel. This is how I did a script called BlockEscape that reads like this:
public CanvasGroup pausePanel;
public bool subMenuClicked = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (subMenuClicked == false)
{
pausePanel.alpha = 1;
pausePanel.interactable = true;
pausePanel.blocksRaycasts = true;
}
else
{
pausePanel.alpha = 0;
pausePanel.interactable = false;
pausePanel.blocksRaycasts = false;
}
}
however that works half way because although it avoids the overlay, it doesn't show the pause panel menu anymore after leaving the audio settings even if I press Escape. Check the following gif:
I don't know what to do next.
Did you try the code in my answer?
Your problems look like they can be solved by my code...
@Darkforge317 yes, I tried your code, but while testing it I could see that what I was trying to fix with a boolean (sub$$anonymous$$enuClicked) you were trying with an Enum. In the end I decided to fix this issue by using the following code:
public CanvasGroup pausePanel; //Invocation to access the interaction and alpha of the panel
public GameObject audioPanel; //Audio Settings Panel
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
if (audioPanel.activeInHierarchy == true)
{
pausePanel.alpha = 0;
pausePanel.interactable = false;
pausePanel.blocksRaycasts = false;
}
else
{
pausePanel.alpha = 1;
pausePanel.interactable = true;
pausePanel.blocksRaycasts = true;
}
}
however this is far from what I wanted to achieve and it feels just like a bandaid because what I wanted was to cancel the Escape key the moment the audio panel would be ON. Right now, the key still works but the content doesn't show because I made it disappear by modyfing the alpha and blocking any possible interaction with it.