处理退出请求

退出

大多数平台都有要求应用程序退出的选项。在桌面系统中,这通常是通过窗口标题栏上的“x”图标来实现的。在 Android 系统上,当在主屏幕上时,后退按钮被用来退出(否则就返回)。

处理通知

On desktop and web platforms, Node receives a special NOTIFICATION_WM_CLOSE_REQUEST notification when quitting is requested from the window manager.

On Android, 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).

备注

NOTIFICATION_WM_GO_BACK_REQUEST isn’t supported on iOS, as iOS devices don’t have a physical Back button.

处理通知的方法如下(在任何节点上):

GDScriptC#

  1. func _notification(what):
  2. if what == NOTIFICATION_WM_CLOSE_REQUEST:
  3. get_tree().quit() # default behavior
  1. public override void _Notification(int what)
  2. {
  3. if (what == NotificationWMCloseRequest)
  4. GetTree().Quit(); // default behavior
  5. }

在开发移动应用程序时, 除非用户在主屏幕上, 否则不需要退出, 因此处理方式有变化.

It is important to note that by default, Godot apps have the built-in behavior to quit when quit is requested from the window manager. This can be changed, so that the user can take care of the complete quitting procedure:

GDScriptC#

  1. get_tree().set_auto_accept_quit(false)
  1. GetTree().AutoAcceptQuit = false;

发送你自己的退出通知

While forcing the application to close can be done by calling SceneTree.quit, doing so will not send the NOTIFICATION_WM_CLOSE_REQUEST to the nodes in the scene tree. Quitting by calling SceneTree.quit will not allow custom actions to complete (such as saving, confirming the quit, or debugging), even if you try to delay the line that forces the quit.

Instead, if you want to notify the nodes in the scene tree about the upcoming program termination, you should send the notification yourself:

GDScriptC#

  1. get_tree().root.propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
  1. GetTree().Root.PropagateNotification((int)NotificationWMCloseRequest);

Sending this notification will inform all nodes about the program termination, but will not terminate the program itself unlike in 3.X. In order to achieve the previous behavior, SceneTree.quit should be called after the notification.

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.