Application.Quit()在Android上不起作用

编程入门 行业动态 更新时间:2024-10-28 14:22:32
本文介绍了Application.Quit()在Android上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当您按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上不起作用

本文发布于:2023-06-11 03:39:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/625902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上不   Application   Quit   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!