0
\$\begingroup\$

By back press on Android, I want to close all the open windows first then exit the game, I see everywhere it's suggested to check input key in Update:

void Update()
{
    if (Input.GetKey(KeyCode.Escape))
    {
        if (AreAllWindowsClosed())
            Input.backButtonLeavesApp = true;
    }
}

But my issue with this is that it's calling the code in every frame and also may handle a single press multiple times!

Isn't there a single BackPressed event in Unity for this simple task?

\$\endgroup\$
4
  • \$\begingroup\$ Why do you say it would it handle a single press multiple times? GetKeyDown returns true only in the first frame of a press. \$\endgroup\$ – DMGregory May 2 '19 at 18:45
  • \$\begingroup\$ @DMGregory Ah, my mistake. I thought it will be true as long as the key is down. this solves half of the issue. \$\endgroup\$ – Blendester May 2 '19 at 19:04
  • \$\begingroup\$ Then you can fire your own event from a script of your choosing. Why do you need something new for this? \$\endgroup\$ – DMGregory May 2 '19 at 19:06
  • \$\begingroup\$ @DMGregory Instead of constantly checking a flag to see if back key is pressed, I want the system/unity raise an event telling the subscribers it's pressed. This is nothing we can do ourselves, is it? \$\endgroup\$ – Blendester May 2 '19 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.