当您按Android手机的返回/返回键时,我正在尝试使应用程序自行退出.
I am trying to make the application quit itself when you press the back/return key of a Android phone.
由于DontDestroyOnLoad,我试图将这段代码更新到我所有场景中存在的gameObject的脚本的更新中:
I tried putting this code in the update of a script attached to a gameObject that is present in all my scenes, because of DontDestroyOnLoad:
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }我刚刚测试了一些东西,看看是否在手机上玩游戏时进入了if.我到达那里. Application.Quit()是这里的问题.我希望有人能帮助我并解释如何退出Android中的应用.
I just tested something to see if I get inside the if, when playing on my phone. And I get there. Application.Quit() is the issue here. I hope someone can help me and explain how I can quit the app in Android.
谢谢! :)
推荐答案
我知道Application.Quit()在编辑器中不起作用,
I know Application.Quit() does not work in the editor,
是的,它在编辑器中不起作用,但是您可以在编辑器中使用UnityEditor.EditorApplication.isPlaying,但是必须将其包装在UNITY_EDITOR宏周围.请参见下面的示例:
Yes, it does not work in the Editor but you can use UnityEditor.EditorApplication.isPlaying in the Editor but you have to wrap it around the UNITY_EDITOR macro. See the example below:
void Quit() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif }
我缩小了范围,似乎Application.Quit()是问题所在. 我不知道为什么,因为在网络上的每个地方都说应该 在Android上工作
I narrowed it down and it seems that Application.Quit() is the issue. I can't figure out why because everywhere on the web it says it should work on Android
Application.Quit()函数应可在Android上使用.如果不是,那就是一个错误.
The Application.Quit() function should work on Android. If it doesn't then that's a bug.
Unity中还有另一个解决方法.
There is another work-around in Unity.
以下是其他解决方法:
1 .在Awake函数中,将 Input.backButtonLeavesApp 设置为true.在Android上按下后退按钮后,它应该自动退出应用.
1. Set Input.backButtonLeavesApp to true in the Awake function. It should automatically exit the app when the back button is pressed on Android.
void Awake() { Input.backButtonLeavesApp = true; }
2 .如果那不起作用,则终止该进程:
2. If that does not work then kill the process:
System.Diagnostics.Process.GetCurrentProcess().Kill();如果这也不起作用,请通过编辑器提交错误报告.
If this does not work too then file for a bug report through the Editor.
更多推荐
Application.Quit()在Android上不起作用
发布评论