Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kangzin · Dec 02, 2020 at 12:33 AM · popup

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
                 );
         };
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kangzin · Dec 09, 2020 at 10:25 PM 0
Share

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. alt text

I really appreciated for your next help. Thanks

gb.png (163.8 kB)
avatar image jackmw94 kangzin · Dec 09, 2020 at 11:36 PM 0
Share

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!

avatar image
0

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...') } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kangzin · Dec 09, 2020 at 10:31 PM 0
Share

How to implement this in my code above? I tried to add and the code was red underline like this alt text

Really appreciate for your next help. Thanks.

2.png (148.4 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges