How to simulate a back button press from the Unity Editor

When working with Android and Windows Phone in Unity Editor I would like to simulate a back button press.

I understand to do this in code:

    void Update () {
        if (Input.GetKeyUp (KeyCode.Escape)) {
                Application.Quit ();
        }
    }

But just pressing ESC on my keyboard does not seem to do anything.

Thanks

You can try adding this:

#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#endif

If I understand correctly of what you are trying to achieve.

1 Like

No I don’t think that is what I want.

I want to press a key on my keyboard while the app is playing in the editor to simulate the Android/Windows Phone BACK button being pressed.

You kind of need both to some degree, if you’re going to test in the editor, Application.Quit() isn’t going to give you results, but stopping the playing will function in the editor. Otherwise you will need to build it out each time so it’s not hosted in the Unity Editor and can respond to Application.Quit method.

You probably want a combo breaker here:

void Update () {
  if (Input.GetKeyDown (KeyCode.Escape)) {
    #if UNITY_EDITOR
    EditorApplication.isPlaying = false;
    #else
    Application.Quit ();
    #endif
    }
}

This way when a user of the application(or developer) presses escape in a frame and is in the editor, the editor stops playing the current scene. If it’s not in the editor it invokes Application.Quit()

Okay now I understand… Application.Quit() does not do anything in the Editor.

Thank you @proandrius for trying to explain it initially to me.

Thank you @Landern for making obvious.