- Home /
Close popup with escape button while popup active
I make prefab object as popup for exit confirmation in android device. I call the prefab with escape (back button on android) and it's succeed. But, how to close this popup again if escape (back button android) is pressed while this popup is active? With this code, if popup is active and I press escape again, the popup will be stackep up. This is my code to call the popup with escape (back button android) in unity. Thanks a lot for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class Quit : MonoBehaviour
{
// Start is called before the first frame update
void Update()
{
Action action = () => {
Application.Quit();
};
if (Input.GetKeyDown(KeyCode.Escape)) {
Popup popup = UIController.Instance.CreatePopup();
//Init popup with params (canvas, text, text, text, action)
popup.Init(UIController.Instance.MainCanvas,
"Are you sure want to exit?",
"No",
"Yes",
action
);
};
}
}
Answer by jackmw94 · Dec 03, 2020 at 09:28 AM
If you want escape to do different behaviour based on whether a popup is already active then you'll need to change the contents of the if-statement to check for this.
if (Input.GetKeyDown(KeyCode.Escape)) {
if (UIController.Instance.IsPopupActive())
{
UIController.Instance.ClosePopup();
}
else
{
Popup popup = UIController.Instance.CreatePopup();
//Init popup with params (canvas, text, text, text, action)
popup.Init(UIController.Instance.MainCanvas,
"Are you sure want to exit?",
"No",
"Yes",
action
);
}
}
There are two functions here you'll need to implement within the UI controller (if you don't already have something similar).
Let me know if you need further clarification!
Hi, thanks for your help before. Your code is still not working.
UIController.Instance.IsPopupActive() and UIController.Instance.ClosePopup(); has error detected with red underline. I think it needs more function description. I just click Potential Fix in Visual Studio Editor and it has added some lines like these automatically.
internal bool IsPopupActive()
{
throw new NotImplementedException();
}
internal void ClosePopup()
{
throw new NotImplementedException();
}
Then I tried to play and nothing happen. Escape function doesn't show my Popup. The codes was automatically added in my UIController.cs script.
I really appreciated for your next help. Thanks
Yeah, those two need implementing.
From your UIController code I can see there are a few edits that need to be made for it to track popups that stack:
Firstly, the UI controller needs to know what gameobject to destroy when we want to close a popup, so we need to store references to the active popup gameobjects in the UI controller class.
// at the top of the class near Instance and $$anonymous$$ainCanvas
private Stack<GameObject> popupStack = new Stack<GameObject>();
Secondly, we need to add and remove from this collection where we create and close popups. So in your CreatePopup function, add a line that to 'Push' the popUpGo to the popupStack. Then in your ClosePopup function, we can 'Pop' the top popup from the stack and Destroy it to close it.
Finally the IsPopupActive function can be implemented by just checking whether there are any object on the popup stack. return popupStack.Count > 0;
After reading your question again I realised that you probably didn't want the popups to stack but thought I should leave this answer because I'd already written it and it's descriptive! This will still work for your case since the code that uses the UI controller ensures the popup doesn't stack. Sorry for the confusion and let me know if you need more help!
Answer by Still544 · Dec 03, 2020 at 01:20 PM
The issue is because the keypress event does not trigger for non-printable characters (eg backspace or escape).
To solve the problem, you could use keydown instead:
function ESCclose(evt) { if (evt.keyCode == 27) { //window.close(); console.log('close the window...') } }
How to implement this in my code above? I tried to add and the code was red underline like this
Really appreciate for your next help. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020734im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to find out how many virtual memory left free on iphone of my user? 1 Answer
How to assign multiple popup textures to game objects 1 Answer
Help with GUI popup 0 Answers
Making a custom popup for data objects: Can I use the PropertyDrawer to change the object itself? 0 Answers
Text box pop up on mouseover 1 Answer