处理退出请求
退出
Most platforms have the option to request the application to quit. On desktops, this is usually done with the “x” icon on the window title bar. On Android, the back button is used to quit when on the main screen (and to go back otherwise).
处理通知
On desktop platforms, the MainLoop has a special MainLoop.NOTIFICATION_WM_QUIT_REQUEST
notification that is sent to all nodes when quitting is requested.
On Android, MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST
is sent instead. Pressing the Back button will exit the application if Application > Config > Quit On Go Back is checked in the Project Settings (which is the default).
注解
MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST
isn’t supported on iOS, as iOS devices don’t have a physical Back button.
Handling the notification is done as follows (on any node):
GDScript
C#
func _notification(what):
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
get_tree().quit() # default behavior
public override void _Notification(int what)
{
if (what == MainLoop.NotificationWmQuitRequest)
GetTree().Quit(); // default behavior
}
在开发移动应用程序时,除非用户在主屏幕上,否则不需要退出,因此处理方式有变化。
值得注意的是,默认情况下,在请求退出时,Godot应用程序具有退出的内置行为,可以更改为:
GDScript
C#
get_tree().set_auto_accept_quit(false)
GetTree().SetAutoAcceptQuit(false);