Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
为尺寸优化构建
依据
有时, 需要针对大小而不是速度来优化构建. 这意味着不编译引擎上的未使用的函数, 以及使用特定的编译器标志来帮助减小构建大小. 常见情况包括为移动和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.
编译时使用连接时间优化
节省空间:高
难度:简单
在官方构建中执行:是
启用链接时优化可以在性能和文件大小方面生成更高效的二进制文件. 它通过消除重复的模板功能和未使用的代码来工作. 目前, 它可以与GCC和MSVC编译器一起使用:
scons target=template_release lto=full
使用此选项时,链接会变得特别慢、消耗更多的内存,所以应该仅用于构建发布版本:
编译
master
分支时,如果启用了 LTO,那么你需要至少 8 GB 可用内存才能成功链接。编译
3.x
分支时,如果启用了 LTO,那么你需要至少 6 GB 可用内存才能成功链接。
针对大小而不是速度优化
节省空间:高
难度:简单
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
节省空间:高
难度:简单
在官方构建中执行:否
默认情况下,Godot使用一个支持以下功能的高级文本服务器:
书写阿拉伯语和希伯来语等语言所需的从右向左的排版和复杂的文字。
字体连字和 OpenType 功能(例如小型大写字母、分数和斜线零)。
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%.
备注
禁用 3D 支持也会禁用所有导航。这包括 2D 导航,因为它在内部使用 3D 导航系统。
禁用高级 GUI 对象
节省空间:一般
难度:简单
在官方构建中执行:否
大多数小游戏不需要复杂的GUI控件, 如Tree, ItemList, TextEdit或GraphEdit. 它们可以用一个构建标志来禁用:
scons target=template_release disable_advanced_gui=yes
以下所有东西都会被禁用:
禁用不需要的模块
节省空间:非常低到一般之间,取决于模块
难度:中等到难之间,取决于模块
在官方构建中执行:否
许多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
来提供一个禁用模块的列表, 其内容类似于以下:
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.
像 Steam 这样的平台已经应用了它们自己的压缩方案,所以你不需要首先创建一个 ZIP 存档来分发文件。
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.
当使用 7-Zip GUI 时,这是通过使用超压缩模式创建 Zip 存档来完成的。使用命令行时,使用以下命令完成此操作:
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.
创建具有高压缩级别的预压缩 gzip 或 Brotli 文件可能会更有效,只要 web 服务器被配置为在这些文件存在时提供服务。如果支持的话,Brotli 应该比 gzip 更受欢迎,因为它在减小文件大小方面有更大的潜力。
详细步骤见 提供文件。