Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

为尺寸优化构建

依据

有时, 需要针对大小而不是速度来优化构建. 这意味着不编译引擎上的未使用的函数, 以及使用特定的编译器标志来帮助减小构建大小. 常见情况包括为移动和Web平台创建构建.

本教程旨在概述创建较小二进制文件的不同方法. 在继续之前, 建议阅读之前有关为每个平台编译Godot的教程.

下面的选项按照从最重要(大小节省得最多)到最不重要(大小节省得最少)的顺序排列。

剥离二进制文件

  • 节省空间:非常高

  • 难度:简单

  • 在官方构建中执行:

如果你从源码构建了 Windows(MinGW)、Linux、macOS 的二进制文件,请记得剥离二进制文件中的调试符号。首先请安装你的发行版中的 strip 包,然后执行:

strip path/to/godot.binary

在 Windows 上,大多数 MinGW 工具链安装时都会包含 strip.exe

这样可以将编译后二进制文件减少到原先五分之一到十分之一的大小。缺点是崩溃追踪中就无法再提供准确的信息了(可用于查找崩溃原因)。C++ 性能分析器也将无法显示函数名称(不影响内置的 GDScript 性能分析器)。

备注

The above command will not work on Windows binaries compiled with MSVC and platforms such as Android and Web. Instead, pass debug_symbols=no on the SCons command line when compiling.

针对大小而不是速度优化

  • 节省空间:

  • 难度:简单

  • Performed in official builds: Yes, but only for web builds

Godot 3.1以上版本允许使用尺寸优化(而不是速度优化)进行编译. 要启用这个功能, 请将 optimize 标志设置为 size :

scons target=template_release optimize=size

某些平台如WebAssembly默认情况下已使用此模式.

Disabling advanced text server

  • 节省空间:

  • 难度:简单

  • 在官方构建中执行:

By default, Godot uses an advanced text server with the support for the following features:

  • Right-to-left typesetting and complex scripts, required to write languages such as Arabic and Hebrew.

  • Font ligatures and OpenType features (such as small capitals, fractions and slashed zero).

Godot provides a fallback text server that isn't compiled by default. This text server can be used as a lightweight alternative to the default advanced text server:

scons target=template_release module_text_server_adv_enabled=no module_text_server_fb_enabled=yes

If you only intend on supporting Latin, Greek and Cyrillic-based languages in your project, the fallback text server should suffice.

This fallback text server can also process large amounts of text more quickly than the advanced text server. This makes the fallback text server a good fit for mobile/web projects.

备注

Remember to always pass module_text_server_fb_enabled=yes when using module_text_server_adv_enabled=no. Otherwise, the compiled binary won't contain any text server, which means no text will be displayed at all when running the project.

禁用 3D

  • 节省空间:一般

  • 难度:简单

  • 在官方构建中执行:

对于 2D 游戏,拥有整个 3D 引擎通常没有任何意义。因此,有一个构建标志来禁用它:

scons target=template_release disable_3d=yes

必须禁用工具才能使用此标志, 因为编辑器不能在没有3D支持的情况下运行. 没有它, 二进制大小可以减少大约15%.

禁用高级 GUI 对象

  • 节省空间:一般

  • 难度:简单

  • 在官方构建中执行:

大多数小游戏不需要复杂的GUI控件, 如Tree, ItemList, TextEdit或GraphEdit. 它们可以用一个构建标志来禁用:

scons target=template_release disable_advanced_gui=yes

以下所有东西都会被禁用:

  • FileDialog

  • PopupMenu

  • Tree

  • TextEdit

  • CodeEdit

  • SyntaxHighlighter

  • CodeHighlighter

  • TreeItem

  • OptionButton

  • SpinBox

  • ColorPicker

  • ColorPickerButton

  • RichTextlabel

  • RichTextEffect

  • CharFXTransform

  • AcceptDialog

  • ConfirmationDialog

  • MarginContainer

  • SubViewportContainer

  • SplitContainer

  • HSplitContainer

  • VSplitContainer

  • GraphNode

  • GraphEdit

禁用不需要的模块

  • 节省空间:非常低到一般之间,取决于模块

  • 难度:中等到难之间,取决于模块

  • 在官方构建中执行:

许多Godot的功能都作为模块提供. 你可以使用以下命令查看模块列表:

scons --help

将显示可以禁用的模块列表以及所有构建选项. 如果你正在开发简单的2D游戏, 则可以禁用其中的许多功能:

scons target=template_release module_basis_universal_enabled=no module_bmp_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_jsonrpc_enabled=no module_ktx_enabled=no module_mbedtls_enabled=no module_meshoptimizer_enabled=no module_minimp3_enabled=no module_mobile_vr_enabled=no module_msdfgen_enabled=no module_multiplayer_enabled=no module_noise_enabled=no module_navigation_enabled=no module_ogg_enabled=no module_openxr_enabled=no module_raycast_enabled=no module_regex_enabled=no module_squish_enabled=no module_svg_enabled=no module_tga_enabled=no module_theora_enabled=no module_tinyexr_enabled=no module_upnp_enabled=no module_vhacd_enabled=no module_vorbis_enabled=no module_webrtc_enabled=no module_websocket_enabled=no module_webxr_enabled=no module_zip_enabled=no

If this proves not to work for your use case, you should review the list of modules and see which ones you actually still need for your game (e.g. you might want to keep networking-related modules, regex support, minimp3/ogg/vorbis to play music, or theora to play videos).

或者, 你也可以通过在源代码的根部创建 custom.py 来提供一个禁用模块的列表, 其内容类似于以下:

# custom.py

module_basis_universal_enabled = "no"
module_bmp_enabled = "no"
module_camera_enabled = "no"
module_csg_enabled = "no"
module_dds_enabled = "no"
module_enet_enabled = "no"
module_gridmap_enabled = "no"
module_hdr_enabled = "no"
module_jsonrpc_enabled = "no"
module_ktx_enabled = "no"
module_mbedtls_enabled = "no"
module_meshoptimizer_enabled = "no"
module_minimp3_enabled = "no"
module_mobile_vr_enabled = "no"
module_msdfgen_enabled= "no"
module_multiplayer_enabled = "no"
module_noise_enabled = "no"
module_navigation_enabled = "no"
module_ogg_enabled = "no"
module_openxr_enabled = "no"
module_raycast_enabled = "no"
module_regex_enabled = "no"
module_squish_enabled = "no"
module_svg_enabled = "no"
module_tga_enabled = "no"
module_theora_enabled = "no"
module_tinyexr_enabled = "no"
module_upnp_enabled = "no"
module_vhacd_enabled = "no"
module_vorbis_enabled = "no"
module_webrtc_enabled = "no"
module_websocket_enabled = "no"
module_webxr_enabled = "no"
module_zip_enabled = "no"

Optimizing the distribution of your project

Desktop

备注

This section is only relevant when distributing the files on a desktop platform that doesn't perform its own compression or packing. As such, this advice is relevant when you distribute ZIP archives on itch.io or GitHub Releases.

Platforms like Steam already apply their own compression scheme, so you don't need to create a ZIP archive to distribute files in the first place.

As an aside, you can look into optimizing the distribution of your project itself. This can be done even without recompiling the export template.

7-Zip can be used to create ZIP archives that are more efficient than usual, while remaining compatible with every ZIP extractor (including Windows' own built-in extractor). ZIP size reduction in a large project can reach dozens of megabytes compared to a typical ZIP compressor, although average savings are in the 1-5 MB range. Creating this ZIP archive will take longer than usual, but it will extract just as fast as any other ZIP archive.

When using the 7-Zip GUI, this is done by creating a ZIP archive with the Ultra compression mode. When using the command line, this is done using the following command:

7z a -mx9 my_project.zip folder_containing_executable_and_pck

Web

Enabling gzip or Brotli compression for all file types from the web export (especially the .wasm and .pck) can reduce the download size significantly, leading to faster loading times, especially on slow connections.

Creating precompressed gzip or Brotli files with a high compression level can be even more efficient, as long as the web server is configured to serve those files when they exist. When supported, Brotli should be preferred over gzip as it has a greater potential for file size reduction.

See 提供文件 for instructions.