3

I would like my app to show the message on the first back button press as "Please touch back button again to quit the app" and when it is pressed again the app should quit. I think I have added appropriate code but it doesn't work.

The script is attached as a component to the canvas element. The script contains the public variable which I assigned the panel(Child of canvas) UI element.

Scene hierarchy

Observed: When I pressed the back button the text appears but only a fraction of a second and then disappear all of a sudden and the next back button press did not resulted in app quit.

Desired On first back button press it should show the message and with in say 3 seconds if the second back button pressed the app should quit.

Relevant information: Unity 2017.1.0f3

Here is the Code link :

https://gist.github.com/bmohanrajbit27/431221fc80e0b247649289fd136f9cfb

public class ChangeSceneScript : MonoBehaviour
{
    private bool iQuit = false;
    public GameObject quitobject;

    void Update()
    {
        if (iQuit == true)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            quitobject.SetActive(true);
            iQuit = true;
            StartCoroutine(QuitingTimer());

        }
    }

    IEnumerator QuitingTimer()
    {
        yield return new WaitForSeconds(3);
        iQuit = false;
        quitobject.SetActive(false);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Mohan
  • 919
  • 1
  • 10
  • 20
  • Seems like you are almost done, try this; when you press the back button first time, make a count down from 3. While this value is bigger than 0, you can check this in a if condition, if back button pressed then quit your app. – Thalthanas Sep 08 '17 at 06:18
  • I ment without using a coroutine – Thalthanas Sep 08 '17 at 06:21
  • 1
    @Mohan Can you try replacing the 12th line **if (Input.GetKeyDown (KeyCode.Escape))** with **else if (Input.GetKeyDown (KeyCode.Escape))** – ZayedUpal Sep 08 '17 at 06:26
  • weird this code should work – Daahrien Sep 08 '17 at 08:00

1 Answers1

5

I've seen few instances where Application.Quit(); did not work on Android. When this happens, use System.Diagnostics.Process.GetCurrentProcess().Kill(); to exit out of the program.

Now, for your timer issue, start a coroutine in the Update function when input is pressed for the first time. Use a flag to make sure that this coroutine function is not started again until the last one has finished. A boolean variable is fine.

Inside, that coroutine function, don't use yield return new WaitForSeconds(3); to wait for the timer. Use a while loop with the combination of yield return null; to wait until the timer is done. Increment the timer with Time.deltaTime each frame. Now, you can easily check for the second press in that coroutine function and exit if pressed.

If you also want this to work in the Editor, you have to use UnityEditor.EditorApplication.isPlaying = false; to exit. The example below should also work in the Editor. See the comments in the code if you have any question.

public GameObject quitobject;
private bool clickedBefore = false;

void Update()
{
    //Check input for the first time
    if (Input.GetKeyDown(KeyCode.Escape) && !clickedBefore)
    {
        Debug.Log("Back Button pressed for the first time");
        //Set to false so that this input is not checked again. It will be checked in the coroutine function instead
        clickedBefore = true;

        //Activate Quit Object
        quitobject.SetActive(true);

        //Start quit timer
        StartCoroutine(quitingTimer());
    }
}

IEnumerator quitingTimer()
{
    //Wait for a frame so that Input.GetKeyDown is no longer true
    yield return null;

    //3 seconds timer
    const float timerTime = 3f;
    float counter = 0;

    while (counter < timerTime)
    {
        //Increment counter while it is < timer time(3)
        counter += Time.deltaTime;

        //Check if Input is pressed again while timer is running then quit/exit if is
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Back Button pressed for the second time. EXITING.....");
            Quit();
        }

        //Wait for a frame so that Unity does not freeze
        yield return null;
    }

    Debug.Log("Timer finished...Back Button was NOT pressed for the second time within: '" + timerTime + "' seconds");

    //Timer has finished and NO QUIT(NO second press) so deactivate
    quitobject.SetActive(false);
    //Reset clickedBefore so that Input can be checked again in the Update function
    clickedBefore = false;
}

void Quit()
{
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
    #else
    //Application.Quit();
    System.Diagnostics.Process.GetCurrentProcess().Kill();
    #endif
}
Programmer
  • 121,791
  • 22
  • 236
  • 328