OS

Inherits: Object

操作系统功能。

描述

操作系统的功能。OS包装了与主机操作系统通信的最常见功能,如剪贴板、视频驱动程序、日期和时间、计时器、环境变量、二进制文件的执行、命令行等。

教程

属性

String

clipboard

“”

int

current_screen

0

bool

delta_smoothing

true

int

exit_code

0

bool

keep_screen_on

true

bool

low_processor_usage_mode

false

int

low_processor_usage_mode_sleep_usec

6900

Vector2

max_window_size

Vector2( 0, 0 )

Vector2

min_window_size

Vector2( 0, 0 )

ScreenOrientation

screen_orientation

0

String

tablet_driver

“”

bool

vsync_enabled

true

bool

vsync_via_compositor

false

bool

window_borderless

false

bool

window_fullscreen

false

bool

window_maximized

false

bool

window_minimized

false

bool

window_per_pixel_transparency_enabled

false

Vector2

window_position

Vector2( 0, 0 )

bool

window_resizable

true

Vector2

window_size

Vector2( 0, 0 )

方法

void

alert ( String text, String title=”Alert!” )

bool

can_draw ( ) const

bool

can_use_threads ( ) const

void

center_window ( )

void

close_midi_inputs ( )

void

crash ( String message )

void

delay_msec ( int msec ) const

void

delay_usec ( int usec ) const

void

dump_memory_to_file ( String file )

void

dump_resources_to_file ( String file )

int

execute ( String path, PoolStringArray arguments, bool blocking=true, Array output=[ ], bool read_stderr=false )

int

find_scancode_from_string ( String string ) const

int

get_audio_driver_count ( ) const

String

get_audio_driver_name ( int driver ) const

String

get_cache_dir ( ) const

PoolStringArray

get_cmdline_args ( )

String

get_config_dir ( ) const

PoolStringArray

get_connected_midi_inputs ( )

VideoDriver

get_current_video_driver ( ) const

String

get_data_dir ( ) const

Dictionary

get_date ( bool utc=false ) const

Dictionary

get_datetime ( bool utc=false ) const

Dictionary

get_datetime_from_unix_time ( int unix_time_val ) const

int

get_dynamic_memory_usage ( ) const

String

get_environment ( String variable ) const

String

get_executable_path ( ) const

PoolStringArray

get_granted_permissions ( ) const

Vector2

get_ime_selection ( ) const

String

get_ime_text ( ) const

String

get_latin_keyboard_variant ( ) const

String

get_locale ( ) const

String

get_locale_language ( ) const

int

get_main_thread_id ( ) const

String

get_model_name ( ) const

String

get_name ( ) const

int

get_native_handle ( HandleType handle_type )

int

get_power_percent_left ( )

int

get_power_seconds_left ( )

PowerState

get_power_state ( )

int

get_process_id ( ) const

int

get_processor_count ( ) const

Vector2

get_real_window_size ( ) const

String

get_scancode_string ( int code ) const

int

get_screen_count ( ) const

int

get_screen_dpi ( int screen=-1 ) const

float

get_screen_max_scale ( ) const

Vector2

get_screen_position ( int screen=-1 ) const

float

get_screen_scale ( int screen=-1 ) const

Vector2

get_screen_size ( int screen=-1 ) const

int

get_splash_tick_msec ( ) const

int

get_static_memory_peak_usage ( ) const

int

get_static_memory_usage ( ) const

String

get_system_dir ( SystemDir dir, bool shared_storage=true ) const

int

get_system_time_msecs ( ) const

int

get_system_time_secs ( ) const

int

get_tablet_driver_count ( ) const

String

get_tablet_driver_name ( int idx ) const

int

get_thread_caller_id ( ) const

int

get_ticks_msec ( ) const

int

get_ticks_usec ( ) const

Dictionary

get_time ( bool utc=false ) const

Dictionary

get_time_zone_info ( ) const

String

get_unique_id ( ) const

int

get_unix_time ( ) const

int

get_unix_time_from_datetime ( Dictionary datetime ) const

String

get_user_data_dir ( ) const

int

get_video_driver_count ( ) const

String

get_video_driver_name ( VideoDriver driver ) const

int

get_virtual_keyboard_height ( )

Rect2

get_window_safe_area ( ) const

void

global_menu_add_item ( String menu, String label, Variant id, Variant meta )

void

global_menu_add_separator ( String menu )

void

global_menu_clear ( String menu )

void

global_menu_remove_item ( String menu, int idx )

bool

has_environment ( String variable ) const

bool

has_feature ( String tag_name ) const

bool

has_touchscreen_ui_hint ( ) const

bool

has_virtual_keyboard ( ) const

void

hide_virtual_keyboard ( )

bool

is_debug_build ( ) const

bool

is_ok_left_and_cancel_right ( ) const

bool

is_scancode_unicode ( int code ) const

bool

is_stdout_verbose ( ) const

bool

is_userfs_persistent ( ) const

bool

is_window_always_on_top ( ) const

bool

is_window_focused ( ) const

int

keyboard_get_current_layout ( ) const

int

keyboard_get_layout_count ( ) const

String

keyboard_get_layout_language ( int index ) const

String

keyboard_get_layout_name ( int index ) const

void

keyboard_set_current_layout ( int index )

Error

kill ( int pid )

void

move_window_to_foreground ( )

bool

native_video_is_playing ( )

void

native_video_pause ( )

Error

native_video_play ( String path, float volume, String audio_track, String subtitle_track )

void

native_video_stop ( )

void

native_video_unpause ( )

void

open_midi_inputs ( )

void

print_all_resources ( String tofile=”” )

void

print_all_textures_by_size ( )

void

print_resources_by_type ( PoolStringArray types )

void

print_resources_in_use ( bool short=false )

void

request_attention ( )

bool

request_permission ( String name )

bool

request_permissions ( )

bool

set_environment ( String variable, String value ) const

void

set_icon ( Image icon )

void

set_ime_active ( bool active )

void

set_ime_position ( Vector2 position )

void

set_native_icon ( String filename )

Error

set_thread_name ( String name )

void

set_use_file_access_save_and_swap ( bool enabled )

void

set_window_always_on_top ( bool enabled )

void

set_window_mouse_passthrough ( PoolVector2Array region )

void

set_window_title ( String title )

Error

shell_open ( String uri )

void

show_virtual_keyboard ( String existing_text=””, bool multiline=false )

枚举

enum VideoDriver:

  • VIDEO_DRIVER_GLES2 = 1 —- GLES2的渲染后端。它在移动设备上使用OpenGL ES 2.0,在桌面平台上使用OpenGL 2.1,在网络上使用WebGL 1.0。

  • VIDEO_DRIVER_GLES3 = 0 —- GLES3 渲染后端。它在移动设备上使用 OpenGL ES 3.0,在桌面平台上使用 OpenGL 3.3,在网络上使用 WebGL 2.0。


enum Weekday:

  • DAY_SUNDAY = 0 —- 星期日。

  • DAY_MONDAY = 1 —- 星期一。

  • DAY_TUESDAY = 2 —- 星期二。

  • DAY_WEDNESDAY = 3 —- 星期三。

  • DAY_THURSDAY = 4 —- 星期四。

  • DAY_FRIDAY = 5 —- 星期五。

  • DAY_SATURDAY = 6 —- 星期六。


enum Month:

  • MONTH_JANUARY = 1 —- 一月。

  • MONTH_FEBRUARY = 2 —- 二月。

  • MONTH_MARCH = 3 —- 三月。

  • MONTH_APRIL = 4 —- 四月。

  • MONTH_MAY = 5 —- 五月。

  • MONTH_JUNE = 6 —- 六月。

  • MONTH_JULY = 7 —- 七月。

  • MONTH_AUGUST = 8 —- 八月。

  • MONTH_SEPTEMBER = 9 —- 九月。

  • MONTH_OCTOBER = 10 —- 十月。

  • MONTH_NOVEMBER = 11 —- 十一月。

  • MONTH_DECEMBER = 12 —- 十二月。


enum HandleType:

  • APPLICATION_HANDLE = 0 —- 应用程序句柄:

  • Windows: HINSTANCE 的应用程序

  • MacOS: NSApplication* 的应用程序(尚未实现)

  • Android: JNIEnv* 的应用程序 (尚未实现)

  • DISPLAY_HANDLE = 1 —- 显示句柄:

  • Linux:X11::Display* 用于显示

  • WINDOW_HANDLE = 2 —- 窗户句柄:

  • Windows:主窗口的 HWND

  • Linux:主窗口的 X11::Window*

  • MacOS:主窗口的 NSWindow*(尚未实现)

  • Android:主 Android activity 的 jObject(尚未实现)

  • WINDOW_VIEW = 3 —- 窗口视图:

  • Windows:主窗口绘图环境的 HDC

  • MacOS:主窗口视图的 NSView*(尚未实现)

  • OPENGL_CONTEXT = 4 —- OpenGL上下文:

  • Windows:HGLRC

  • Linux:X11::GLXContext

  • MacOS:NSOpenGLContext* (尚未实现)


enum ScreenOrientation:

  • SCREEN_ORIENTATION_LANDSCAPE = 0 —- 横向屏幕方向。

  • SCREEN_ORIENTATION_PORTRAIT = 1 —- 纵向屏幕方向。

  • SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 2 —- 反转横向屏幕方向。

  • SCREEN_ORIENTATION_REVERSE_PORTRAIT = 3 —- 反转纵向屏幕方向。

  • SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 4 —- 根据硬件传感器使用横向或反向横向。

  • SCREEN_ORIENTATION_SENSOR_PORTRAIT = 5 —- 根据硬件传感器使用纵向或反向纵向。

  • SCREEN_ORIENTATION_SENSOR = 6 —- 根据硬件传感器使用最合适的方向。


enum SystemDir:

  • SYSTEM_DIR_DESKTOP = 0 —- 桌面目录路径。

  • SYSTEM_DIR_DCIM = 1 —- DCIM(数码相机图像)目录路径。

  • SYSTEM_DIR_DOCUMENTS = 2 —- 文档目录路径。

  • SYSTEM_DIR_DOWNLOADS = 3 —- 下载目录路径。

  • SYSTEM_DIR_MOVIES = 4 —- 影片目录路径。

  • SYSTEM_DIR_MUSIC = 5 —- 音乐目录路径。

  • SYSTEM_DIR_PICTURES = 6 —- 图片目录路径。

  • SYSTEM_DIR_RINGTONES = 7 —- 铃声目录路径。


enum PowerState:

  • POWERSTATE_UNKNOWN = 0 —- 未知电源状态。

  • POWERSTATE_ON_BATTERY = 1 —- 拔掉插头,用电池运行。

  • POWERSTATE_NO_BATTERY = 2 —- 插上电源,没有电池可用。

  • POWERSTATE_CHARGING = 3 —- 插上电源,电池正在充电。

  • POWERSTATE_CHARGED = 4 —- 插上电源,电池充满电。

属性说明

Default

“”

Setter

set_clipboard(value)

Getter

get_clipboard()

主机操作系统的剪贴板在某些平台上可能不可用。


  • int current_screen

Default

0

Setter

set_current_screen(value)

Getter

get_current_screen()

当前屏幕索引(从 0 开始)。


  • bool delta_smoothing

Default

true

Setter

set_delta_smoothing(value)

Getter

is_delta_smoothing_enabled()

如果 true,引擎会过滤每帧之间测量的时间增量,并尝试补偿随机变化。这只会在垂直同步处于活动状态的系统上运行。


Default

0

Setter

set_exit_code(value)

Getter

get_exit_code()

主循环退出时传递给操作系统的退出代码。按照惯例,一个0的退出代码表示成功,而一个非零的退出代码表示错误。出于可移植性的考虑,退出代码应该设置在0到125之间(包括125)。

注意: 如果使用SceneTree.quit,并传递exit_code参数,这个值将被忽略。


  • bool keep_screen_on

Default

true

Setter

set_keep_screen_on(value)

Getter

is_keep_screen_on()

如果 true,引擎会尝试在游戏运行时保持屏幕开启。在手机上有用。


  • bool low_processor_usage_mode

Default

false

Setter

set_low_processor_usage_mode(value)

Getter

is_in_low_processor_usage_mode()

如果true,引擎会通过只在需要时刷新屏幕来优化处理器的使用。可以改善移动设备上的电池消耗。


  • int low_processor_usage_mode_sleep_usec

Default

6900

Setter

set_low_processor_usage_mode_sleep_usec(value)

Getter

get_low_processor_usage_mode_sleep_usec()

启用低处理器使用模式时,帧之间的休眠量(以微秒计)。较高的值将导致较低的CPU使用率。


Default

Vector2( 0, 0 )

Setter

set_max_window_size(value)

Getter

get_max_window_size()

窗口的最大大小(不包括窗口管理器装饰)。不影响全屏模式。设置为 (0, 0) 可重置为系统默认值。


Default

Vector2( 0, 0 )

Setter

set_min_window_size(value)

Getter

get_min_window_size()

窗口的最小尺寸(以像素为单位)(不计算窗口管理器装饰)。不影响全屏模式。设置为 (0, 0) 可重置为系统默认值。

注: 默认情况下,项目窗口的最小尺寸为 Vector2(64, 64)。这可以防止在将窗口大小调整为接近零时可能出现的问题。


Default

0

Setter

set_screen_orientation(value)

Getter

get_screen_orientation()

当前屏幕方向。


Default

“”

Setter

set_current_tablet_driver(value)

Getter

get_current_tablet_driver()

当前正在使用的书写板(译注:或数位板、触摸板,尚未定论)驱动程序。


Default

true

Setter

set_use_vsync(value)

Getter

is_vsync_enabled()

如果 true,则启用垂直同步 (Vsync)。


  • bool vsync_via_compositor

Default

false

Setter

set_vsync_via_compositor(value)

Getter

is_vsync_via_compositor_enabled()

true 时,如果 vsync_enabled 为真,当操作系统的窗口合成器被启用并且游戏处于窗口模式时,将使用该合成器进行垂直同步。

注意:此选项是实验性的,旨在缓解某些用户遇到的卡顿。但是,一些用户在使用时遇到了 Vsync 帧率减半,例如从 60 FPS 到 30 FPS。

注意:此属性仅在 Windows 上实现。


  • bool window_borderless

Default

false

Setter

set_borderless_window(value)

Getter

get_borderless_window()

如果 true,则移除窗框。

注意:window_borderless 设置为 false 将禁用逐像素透明度。


  • bool window_fullscreen

Default

false

Setter

set_window_fullscreen(value)

Getter

is_window_fullscreen()

如果true,窗口为全屏。


  • bool window_maximized

Default

false

Setter

set_window_maximized(value)

Getter

is_window_maximized()

如果true,窗口被最大化。


  • bool window_minimized

Default

false

Setter

set_window_minimized(value)

Getter

is_window_minimized()

如果true,窗口被最小化。


  • bool window_per_pixel_transparency_enabled

Default

false

Setter

set_window_per_pixel_transparency_enabled(value)

Getter

get_window_per_pixel_transparency_enabled()

如果true,则窗口背景是透明的,窗口框架被移除。

使用 get_tree().get_root().set_transparent_background(true) 禁用主视口背景渲染。

注意:如果禁用ProjectSettings.display/window/per_pixel_transparency/allowed设置,则该属性无效。

注意: 此属性在 HTML5、Linux、macOS、Windows 和 Android 上实现。对于 Android,它不能在运行时更改。使用 ProjectSettings.display/window/per_pixel_transparency/enabled 在启动时进行设置。


Default

Vector2( 0, 0 )

Setter

set_window_position(value)

Getter

get_window_position()

窗口相对于屏幕的位置,原点为左上角,+Y 轴向下,+X 轴向右。


  • bool window_resizable

Default

true

Setter

set_window_resizable(value)

Getter

is_window_resizable()

如果 true,用户可以调整窗口大小。


Default

Vector2( 0, 0 )

Setter

set_window_size(value)

Getter

get_window_size()

窗口的大小(不包括窗口管理器装饰)。

方法说明

使用主机操作系统显示一个模式化的对话框。执行将被阻塞,直到该对话框被关闭。


  • bool can_draw ( ) const

如果主机操作系统允许绘制,则返回 true


  • bool can_use_threads ( ) const

如果当前主机平台使用多个线程,则返回true


  • void center_window ( )

如果处于窗口模式,则使窗口在屏幕上居中。


  • void close_midi_inputs ( )

关闭系统MIDI驱动程序。

注意:该方法只在Linux, macOS和Windows上实现。


  • void crash ( String message )

Crashes the engine (or the editor if called within a tool script). This should only be used for testing the system’s crash handler, not for any other purpose. For general error reporting, use (in order of preference) @GDScript.assert, @GDScript.push_error or alert. See also kill.


  • void delay_msec ( int msec ) const

将当前线程的执行延迟 msec 毫秒。 msec 必须大于或等于 0。否则, delay_msec 将不执行任何操作并打印错误消息。

注: delay_msec是一种阻塞延迟代码执行的方式。要以非阻塞方式延迟代码执行,请参阅 SceneTree.create_timer。使用 SceneTree.create_timer 生成将延迟位于 yield 下方的代码的执行,而不会影响项目的其余部分(或编辑器,例如 EditorPluginEditorScript)。

注意:当在主线程上调用delay_msec时,它会冻结项目并阻止它重新绘制和注册输入,直到延迟结束。当使用 delay_msec 作为 EditorPluginEditorScript 的一部分时,它会冻结编辑器但不会冻结当前正在运行的项目(因为项目是一个独立的子进程)。


  • void delay_usec ( int usec ) const

将当前线程的执行延迟 usec 微秒。 usec 必须大于或等于 0。否则, delay_usec 将什么也不做,并会打印错误消息。

注: delay_usec是一种阻塞延迟代码执行的方式。要以非阻塞方式延迟代码执行,请参阅 SceneTree.create_timer。使用 SceneTree.create_timer 生成将延迟位于 yield 下方的代码的执行,而不会影响项目的其余部分(或编辑器,例如 EditorPluginEditorScript)。

注意:当在主线程上调用delay_usec时,它会冻结项目并阻止它重绘和注册输入,直到延迟结束。当使用 delay_usec 作为 EditorPluginEditorScript 的一部分时,它会冻结编辑器但不会冻结当前正在运行的项目(因为项目是一个独立的子进程)。


  • void dump_memory_to_file ( String file )

将内存分配ringlist转储到一个文件(仅在调试中工作)。

每行输入格式:“地址-大小-描述”。


  • void dump_resources_to_file ( String file )

将所有使用的资源转储到文件中(仅在调试中有效)。

每行输入格式:“资源类型:资源位置”。

在文件的末尾是所有已使用资源类型的统计数据。


在给定的路径上执行文件,参数以字符串数组的形式传递。将进行平台路径解析。被解析的文件必须存在并且是可执行的。

参数按照给定的顺序使用,并用空格隔开,所以OS.execute("ping", ["-w", "3", "godotengine.org"], false) 将在系统的外壳中解析为ping -w 3 godotengine.org

这个方法根据是否启用blocking模式,有稍微不同的行为。

如果blockingtrue,Godot线程将暂停执行,等待进程的终止。进程的shell输出将作为一个字符串写入output数组。当进程终止时,Godot线程将恢复执行。

如果blockingfalse,Godot线程将在新进程运行时继续。在非阻塞模式下不可能检索shell的输出,所以output将是空的。

返回值也取决于阻塞模式。当阻塞时,该方法将返回一个进程的退出代码。当非阻塞时,该方法返回一个进程ID,你可以用它来监视该进程(并有可能用kill来终止它)。如果进程分叉(非阻塞)或打开(阻塞)失败,该方法将返回-1或其他退出代码。

阻塞模式和检索shell输出的例子:

  1. var output = []
  2. var exit_code = OS.execute("ls", ["-l", "/tmp"], true, output)

Example of non-blocking mode, running another instance of the project and storing its process ID:

  1. var pid = OS.execute(OS.get_executable_path(), [], false)

If you wish to access a shell built-in or perform a composite command, a platform-specific shell can be invoked. For example:

  1. OS.execute("CMD.exe", ["/C", "cd %TEMP% && dir"], true, output)

注:此方法仅在Android, iOS, Linux, macOS和Windows上实现。


  • int find_scancode_from_string ( String string ) const

返回给定字符串的扫描码(例如“Escape”)。


  • int get_audio_driver_count ( ) const

返回可用音频驱动程序的总数。


  • String get_audio_driver_name ( int driver ) const

返回给定索引的音频驱动程序名称。


  • String get_cache_dir ( ) const

Returns the global cache data directory according to the operating system’s standards. On desktop platforms, this path can be overridden by setting the XDG_CACHE_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_config_dir and get_data_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


返回传递给引擎的命令行参数。

命令行参数可以以任何形式写入,包括--key value--key=value两种形式,均可以正确解析,只要自定义的命令行参数不与引擎参数冲突。

你也可以使用get_environment方法包含环境变量。

你可以设置ProjectSettings.editor/main_run_args来定义命令行参数,以便在运行项目时由编辑器传递。

下面是一个小的例子,说明如何使用参数的--key=value形式将命令行参数解析成一个字典。

  1. var arguments = {}
  2. for argument in OS.get_cmdline_args():
  3. if argument.find("=") > -1:
  4. var key_value = argument.split("=")
  5. arguments[key_value[0].lstrip("--")] = key_value[1]

  • String get_config_dir ( ) const

Returns the global user configuration directory according to the operating system’s standards. On desktop platforms, this path can be overridden by setting the XDG_CONFIG_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_cache_dir and get_data_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


返回MIDI设备名称数组。

如果系统MIDI驱动程序之前没有使用open_midi_inputs初始化,返回的数组将是空的。

注意:该方法仅在Linux, macOS和Windows上实现。


返回当前使用的视频驱动程序,使用VideoDriver中的一个值。


  • String get_data_dir ( ) const

Returns the global user data directory according to the operating system’s standards. On desktop platforms, this path can be overridden by setting the XDG_DATA_HOME environment variable before starting the project. See File paths in Godot projects in the documentation for more information. See also get_cache_dir and get_config_dir.

Not to be confused with get_user_data_dir, which returns the project-specific user data path.


Deprecated, use Time.get_date_dict_from_system instead.

Returns current date as a dictionary of keys: year, month, day, weekday, dst (Daylight Savings Time).


Deprecated, use Time.get_datetime_dict_from_system instead.

Returns current datetime as a dictionary of keys: year, month, day, weekday, dst (Daylight Savings Time), hour, minute, second.


  • Dictionary get_datetime_from_unix_time ( int unix_time_val ) const

Deprecated, use Time.get_datetime_dict_from_unix_time instead.

Gets a dictionary of time values corresponding to the given UNIX epoch time (in seconds).

The returned Dictionary’s values will be the same as get_datetime, with the exception of Daylight Savings Time as it cannot be determined from the epoch.


  • int get_dynamic_memory_usage ( ) const

返回使用的动态内存总量(仅适用于调试)。


返回环境变量的值。如果环境变量不存在,则返回一个空字符串。

注意: 仔细检查 variable 的大小写。环境变量名称在除 Windows 之外的所有平台上都区分大小写。


  • String get_executable_path ( ) const

返回当前引擎可执行文件的路径。


通过这个函数,你可以获得已经授予Android应用程序的危险权限列表。

注意: 这个方法在Android上实现。


  • Vector2 get_ime_selection ( ) const

返回相对于组合字符串中的字符的 IME 光标位置(字符串的当前编辑部分)。

MainLoop.NOTIFICATION_OS_IME_UPDATE 被发送到应用程序以通知它 IME 光标位置的变化。

注:此方法在macOS上实现。


  • String get_ime_text ( ) const

返回 IME 中间组合字符串。

MainLoop.NOTIFICATION_OS_IME_UPDATE 被发送到应用程序以通知它对 IME 组合字符串的更改。

注:此方法在macOS上实现。


  • String get_latin_keyboard_variant ( ) const

将当前拉丁键盘变体作为字符串返回。

可能的返回值是: "QWERTY""AZERTY""QZERTY""DVORAK""NEO""COLEMAK""错误ERROR"

注意: 此方法在 Linux、macOS 和 Windows 上实现。在不受支持的平台上返回 "QWERTY"


将主机操作系统区域设置为 language_Script_COUNTRY_VARIANT@extra 形式的字符串。如果您只想要语言代码而不是操作系统中完全指定的语言环境,您可以使用 get_locale_language

language - 2 个或 3 个字母的语言代码,小写。

Script - 可选,4 个字母的文字代码,首字母大写。

COUNTRY - 可选,2 个或 3 个字母 国家地区代码,大写。

VARIANT - 可选,语言变体,地区和排序顺序。 变体可以有任意数量的带下划线的关键字。

extra - 可选,分号分隔的附加关键字列表。货币、日历、排序顺序和编号系统信息。


  • String get_locale_language ( ) const

将主机操作系统区域设置的 2 或 3 个字母 语言代码 作为字符串返回,该字符串应在所有平台上保持一致。这相当于提取 get_locale 字符串的 language 部分。

当您不需要有关国家/地区代码或变体的附加信息时,这可用于将完全指定的区域设置字符串缩小为“通用”语言代码。例如,对于使用 fr_CA 语言环境的加拿大法语用户,这将返回 fr


  • int get_main_thread_id ( ) const

Returns the ID of the main thread. See get_thread_caller_id.

Note: Thread IDs are not deterministic and may be reused across application restarts.


  • String get_model_name ( ) const

返回当前设备的模型名称。

注意:此方法仅在Android和iOS上实现。在不支持的平台上返回"GenericDevice"


返回主机操作系统的名称。可能的值有: "Android", "iOS", "HTML5", "OSX", "Server", "Windows", "UWP", "X11".


返回内部结构指针,以便在GDNative插件中使用。

注意:此方法在Linux和Windows上实现(其他操作系统将很快被支持)。


  • int get_power_percent_left ( )

以百分比形式返回设备中剩余的电池电量。如果电源状态未知,则返回 -1

注意:该方法在Linux、macOS和Windows上实现。


  • int get_power_seconds_left ( )

返回设备耗尽电池前几秒钟内剩余时间的估计值。如果电源状态未知,则返回 -1

注意: 此方法在 Linux、macOS 和 Windows 上实现。


返回设备关于电池和电源的当前状态。请参阅 PowerState 常量。

注意:该方法在Linux、macOS和Windows上实现。


  • int get_process_id ( ) const

返回项目的进程 ID。

注意:此方法在Android、iOS、Linux、macOS和Windows上实现。


  • int get_processor_count ( ) const

返回宿主机上可用的线程数。


  • Vector2 get_real_window_size ( ) const

返回窗口大小,包括窗口边框等装饰。


  • String get_scancode_string ( int code ) const

将给定的扫描码作为字符串返回(例如返回值:"Escape""Shift+Escape")。

另请参阅 InputEventKey.scancodeInputEventKey.get_scancode_with_modifiers


  • int get_screen_count ( ) const

返回连接到宿主机的显示器数量。


  • int get_screen_dpi ( int screen=-1 ) const

返回指定屏幕的每英寸点数密度。如果 screen-1(默认值),将使用当前屏幕。

注意:在 macOS 上,如果使用小数显示缩放模式,则返回值不准确。

注意:在 Android 设备上,实际屏幕密度被归为六种广义密度:

  1. ldpi - 120 dpi
  2. mdpi - 160 dpi
  3. hdpi - 240 dpi
  4. xhdpi - 320 dpi
  5. xxhdpi - 480 dpi
  6. xxxhdpi - 640 dpi

注意:此方法在 Android、Linux、macOS 和 Windows 上实现。在不受支持的平台上返回 72


  • float get_screen_max_scale ( ) const

返回最大的屏幕的缩放。

注: 在macOS上,如果系统中至少有一个hiDPI(Retina)屏幕,则返回值为2.0,其他情况为1.0

注意:此方法仅在macOS上实现。


  • Vector2 get_screen_position ( int screen=-1 ) const

返回索引所指定屏幕的位置。如果 screen-1(默认值),将使用当前屏幕。


  • float get_screen_scale ( int screen=-1 ) const

返回索引所指定屏幕的缩放系数。如果 screen-1(默认值),将使用当前屏幕。

注意:在 macOS 上,对于 hiDPI(Retina)屏幕,返回值是 2.0,对于所有其他情况,返回值是 1.0

注意:此方法在 macOS 上实现。


返回指定屏幕的尺寸,单位为像素。如果 screen-1(默认值),将使用当前屏幕。


  • int get_splash_tick_msec ( ) const

返回启动标志出现所花费的时间(以毫秒为单位)。


  • int get_static_memory_peak_usage ( ) const

返回使用的静态内存的最大数量(仅在调试中有效)。


  • int get_static_memory_usage ( ) const

返回程序所使用的静态内存量,以字节为单位。


返回不同平台上常用文件夹的实际路径。可用的位置在SystemDir中指定。

注意: 这个方法在Android、Linux、macOS和Windows上实现。

注意: 共享存储在Android上实现,并允许区分应用程序特定目录和共享目录。共享目录在Android上有额外的限制。


  • int get_system_time_msecs ( ) const

以毫秒为单位返回操作系统的纪元时间。


  • int get_system_time_secs ( ) const

以秒为单位返回操作系统的纪元时间。


  • int get_tablet_driver_count ( ) const

返回可用的写字板(译注:或触摸板、数位板,在此处尚未明确)驱动程序的总数。

注意:该方法是在Windows上实现的。


  • String get_tablet_driver_name ( int idx ) const

返回给定索引的写字板(译注:或触摸板、数位板,在此处尚未明确)驱动程序名称。

注意:该方法是在Windows上实现的。


  • int get_thread_caller_id ( ) const

返回当前线程的 ID。这可用于日志,以简化多线程应用程序的调试。

注: 线程 ID 不是确定的,也许会在应用程序重新启动时被重复使用。


  • int get_ticks_msec ( ) const

Deprecated, use Time.get_ticks_msec instead.

Returns the amount of time passed in milliseconds since the engine started.


  • int get_ticks_usec ( ) const

Deprecated, use Time.get_ticks_usec instead.

Returns the amount of time passed in microseconds since the engine started.


Deprecated, use Time.get_time_dict_from_system instead.

Returns current time as a dictionary of keys: hour, minute, second.


以字典形式返回当前时区,键为:bias和name。


  • String get_unique_id ( ) const

返回设备所特有的字符串。

注意: 如果用户重新安装/升级其操作系统或更改其硬件,此字符串可能会在不通知的情况下更改。这意味着它通常不应用于加密持续数据,因为在意外的 ID 更改变得无法访问之前保存的数据。返回的字符串也可能使用外部程序伪造,因此出于安全目的,不要依赖 get_unique_id 返回的字符串。

注意: 返回 HTML5 和 UWP 上的空字符串,因为此方法尚未在这些平台上实施。


  • int get_unix_time ( ) const

以秒为单位返回当前的 UNIX 纪元时间戳。

重要: 这是用户可以手动设置的系统时钟。 永远不要使用这种方法进行精确的时间计算,因为它的结果也会受到操作系统的自动调整。 始终使用 get_ticks_usecget_ticks_msec 进行精确时间计算,因为它们保证是单调的(即永不减少)。


从时间值字典中获取纪元时间值。

datetime 必须填充以下键:yearmonthdayhourminutesecond

如果字典为空,则返回 0。如果某些键被省略,它们默认为 UNIX 纪元时间戳 0(1970-01-01 在 00:00:00 UTC)的等效值。

你可以将 get_datetime_from_unix_time 的输出直接传递给此函数。夏令时 (dst)(如果存在)将被忽略。


  • String get_user_data_dir ( ) const

Returns the absolute directory path where user data is written (user://).

On Linux, this is ~/.local/share/godot/app_userdata/[project_name], or ~/.local/share/[custom_name] if use_custom_user_dir is set.

On macOS, this is ~/Library/Application Support/Godot/app_userdata/[project_name], or ~/Library/Application Support/[custom_name] if use_custom_user_dir is set.

On Windows, this is %APPDATA%\Godot\app_userdata\[project_name], or %APPDATA%\[custom_name] if use_custom_user_dir is set. %APPDATA% expands to %USERPROFILE%\AppData\Roaming.

If the project name is empty, user:// falls back to res://.

Not to be confused with get_data_dir, which returns the global (non-project-specific) user data directory.


  • int get_video_driver_count ( ) const

返回当前平台上支持的视频驱动器的数量。


返回与给定 driver 索引匹配的视频驱动程序的名称。此索引是来自 VideoDriver 的值,您可以使用 get_current_video_driver 获取当前后端的索引。


  • int get_virtual_keyboard_height ( )

返回键盘在屏幕上的高度,单位为像素。如果没有键盘或当前键盘被隐藏,则返回0。


  • Rect2 get_window_safe_area ( ) const

返回应呈现交互式控件的窗口的未遮挡区域。


将带有文本“标签”的新项目添加到全局菜单。使用“_dock”菜单将项目添加到 macOS 停靠栏图标菜单。

注:此方法在macOS上实现。


  • void global_menu_add_separator ( String menu )

在项目之间添加一个分隔符。分隔符也占用一个索引。

注意: 这个方法在macOS上实现。


  • void global_menu_clear ( String menu )

清除全局菜单,实际上是删除所有项目。

注意:这个方法在macOS上实现。


  • void global_menu_remove_item ( String menu, int idx )

将索引为 “idx” 的项目从全局菜单中移除。注意,在被删除的项目之后的项目的索引将被移动1位。

注意: 这个方法在macOS上实现。


如果名称为 variable 的环境变量存在,则返回 true

注意: 仔细检查 variable 的大小写。环境变量名称在除 Windows 之外的所有平台上都区分大小写。


Returns true if the feature for the given feature tag is supported in the currently running instance, depending on the platform, build etc. Can be used to check whether you’re currently running a debug build, on a certain platform or arch, etc. Refer to the Feature Tags documentation for more details.

Note: Tag names are case-sensitive.


  • bool has_touchscreen_ui_hint ( ) const

如果设备有触摸屏或模拟有触摸屏,则返回true


  • bool has_virtual_keyboard ( ) const

如果平台有虚拟键盘,返回true,否则返回false


  • void hide_virtual_keyboard ( )

如果虚拟键盘为显示状态则隐藏虚拟键盘,否则不做任何操作。


  • bool is_debug_build ( ) const

如果用于运行项目的Godot二进制文件是debug导出,或在编辑器中运行时,返回true

如果用于运行项目的Godot二进制文件是release导出,则返回false

要检查用于运行项目的Godot二进制文件是否是被导出版本(调试或发布),请使用OS.has_feature("standalone")代替。


  • bool is_ok_left_and_cancel_right ( ) const

如果OK按钮应该显示在左边,Cancel显示在右边,则返回true


  • bool is_scancode_unicode ( int code ) const

如果输入的键码与Unicode字符对应,则返回true


  • bool is_stdout_verbose ( ) const

如果引擎是用-v(verbose stdout)执行的,返回true


  • bool is_userfs_persistent ( ) const

trueuser:// 文件系统是持久的,因此在玩家退出并再次启动游戏后,其状态是相同的。与 HTML5 平台有关,在那里这种持久性可能不可用。


  • bool is_window_always_on_top ( ) const

如果该窗口应总是在其他窗口之上,则返回true


  • bool is_window_focused ( ) const

如果窗口当前获得焦点,则返回true

注意: 只在桌面平台上实现。在其他平台上,它将总是返回true


  • int keyboard_get_current_layout ( ) const

返回活动键盘布局索引。

注意:本方法在Linux、macOS和Windows上实现。


  • int keyboard_get_layout_count ( ) const

返回键盘布局的数量。

注意:本方法在Linux、macOS和Windows上实现。


  • String keyboard_get_layout_language ( int index ) const

返回位于 index 位置的键盘布局的 ISO-639/BCP-47 语言代码。

注意:本方法在 Linux、macOS 和 Windows 上实现。


  • String keyboard_get_layout_name ( int index ) const

返回位于index位置的键盘布局的本地名称。

注意: 本方法可在Linux、macOS和Windows上实现。


  • void keyboard_set_current_layout ( int index )

设置活动键盘布局。

注:此方法可在Linux、macOS和Windows上实现。


Kill (terminate) the process identified by the given process ID (pid), e.g. the one returned by execute in non-blocking mode. See also crash.

Note: This method can also be used to kill processes that were not spawned by the game.

Note: This method is implemented on Android, iOS, Linux, macOS and Windows.


  • void move_window_to_foreground ( )

将窗口移到前面。

注意:这个方法在 Linux、macOS 和 Windows 上实现。


  • bool native_video_is_playing ( )

如果本地视频正在播放,返回true

注意: 这个方法只在iOS上实现。


  • void native_video_pause ( )

暂停本地视频播放。

注意: 这个方法只在iOS上实现。


以给定的音量、音频和字幕轨道播放来自指定路径的本地视频。

注意: 这个方法只在iOS上实现。


  • void native_video_stop ( )

停止本地视频播放。

注意: 这个方法在iOS上实现。


  • void native_video_unpause ( )

恢复本地视频播放。

注意: 这个方法在iOS上实现。


  • void open_midi_inputs ( )

初始化系统MIDI驱动的单例。

注意: 该方法在Linux、macOS和Windows上实现。


  • void print_all_resources ( String tofile=”” )

显示游戏中的所有资源。也可以通过在tofile中指定文件路径将该列表写入文件。


  • void print_all_textures_by_size ( )

显示加载的纹理在内存中按大小排序的列表。


显示游戏加载的给定类型资源的数量。


  • void print_resources_in_use ( bool short=false )

显示游戏当前使用的所有资源。


  • void request_attention ( )

要求用户注意该窗口。它会在Windows上闪烁任务栏按钮,或在OSX上弹出Dock图标。

注意: 这个方法在Linux、macOS和Windows上实现。


目前,这个函数只被AudioDriverOpenSL用来请求Android上RECORD_AUDIO的权限。


  • bool request_permissions ( )

通过这个功能,你可以申请危险的权限,因为在Android应用程序中,正常的权限会在安装时自动授予。

注意: 此方法在Android上实现。


将环境变量variable的值设置为value。在运行set_environment后,环境变量将被设置为Godot进程和任何用execute执行的进程。环境变量将持续存在于Godot进程终止后运行的进程中。

注意: 仔细检查variable的大小写。除Windows外,环境变量名称在所有平台上都是区分大小写的。


  • void set_icon ( Image icon )

使用图像Image资源设置游戏的图标。

同样的图像用于窗口标题、任务栏/dock和窗口选择对话框。图像会根据需要进行缩放。

注意: 这个方法在HTML5、Linux、macOS和Windows上实现。


  • void set_ime_active ( bool active )

设置是否应启用 IME 输入法模式。

如果激活,IME输入法编辑器在应用程序之前处理关键事件并创建组合字符串和建议列表。

应用程序可以通过使用 get_ime_selectionget_ime_text 函数来检索组合状态。

输入完成时提交完成的组合字符串。

注:该方法在Linux、macOS和Windows上实现。


  • void set_ime_position ( Vector2 position )

设置 IME 建议列表弹出窗口的位置(在窗口坐标中)。

注意: 此方法在 Linux、macOS 和 Windows 上实现。


  • void set_native_icon ( String filename )

使用多尺寸平台特定图标文件(*.ico 在 Windows 上设置游戏图标,在 macOS 上设置 *.icns ) 。

适当的大小子图标用于窗口标题、任务栏/停靠面板和窗口选择对话框。

注意: 此方法在 macOS 和 Windows 上实现。


设置当前线程的名称。


  • void set_use_file_access_save_and_swap ( bool enabled )

如果enabledtrue,则启用备份保存。


  • void set_window_always_on_top ( bool enabled )

设置窗口是否应始终位于顶部。

注: 该方法在 Linux、macOS 和 Windows 上实现。


设置窗口的一个接受鼠标事件的多边形区域。该区域外的鼠标事件将被传递出去。

传递一个空数组将禁用穿透支持(所有鼠标事件将被窗口拦截,这是默认行为)。

  1. # 设置区域,使用Path2D节点。
  2. OS.set_window_mouse_passthrough($Path2D.curve.get_baked_points() )
  3. # 设置区域,使用Polygon2D节点。
  4. OS.set_window_mouse_passthrough($Polygon2D.polygon)
  5. # 重置区域为默认值。
  6. OS.set_window_mouse_passthrough([] )

注意:在Windows上,位于区域外的窗口部分不会被绘制,而在Linux和macOS上则会。

注意: 这个方法在Linux、macOS和Windows上实现。


  • void set_window_title ( String title )

将窗口标题设置为指定的字符串。

注意: 这个应该偶尔使用。不要每帧都设置这个,因为这会对某些窗口管理器的性能产生负面影响。

注: 该方法在 HTML5、Linux、macOS 和 Windows 上实现。


Requests the OS to open a resource with the most appropriate program. For example:

  • OS.shell_open("C:\\Users\name\Downloads") on Windows opens the file explorer at the user’s Downloads folder.

  • OS.shell_open("https://godotengine.org") opens the default web browser on the official Godot website.

  • OS.shell_open("mailto:example@example.com") opens the default email client with the “To” field set to example@example.com. See Customizing [code]mailto:[/code] Links for a list of fields that can be added.

Use ProjectSettings.globalize_path to convert a res:// or user:// path into a system path for use with this method.

Note: This method is implemented on Android, iOS, HTML5, Linux, macOS and Windows.


  • void show_virtual_keyboard ( String existing_text=””, bool multiline=false )

如果平台有虚拟键盘,则显示虚拟键盘。

existing_text 参数对于实现您自己的 LineEditTextEdit 很有用,因为它告诉虚拟键盘已经输入了哪些文本(虚拟键盘使用它进行自动更正和预测)。

multiline参数需要设置为true才能输入多行文本,如TextEdit

注:此方法在Android、iOS和UWP上实现。