为尺寸优化构建

解释

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

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

The options below are listed from the most important (greatest size savings) to the least important (lowest size savings).

剥离二进制文件

  • Space savings: Very high

  • Difficulty: Easy

  • Performed in official builds: Yes

If you build Windows (MinGW), Linux or macOS binaries from source, remember to strip debug symbols from binaries by installing the strip package from your distribution then running:

strip path/to/godot.binary

On Windows, strip.exe is included in most MinGW toolchain setups.

This will reduce the size of compiled binaries by a factor between 5× and 10×. The downside is that crash backtraces will no longer provide accurate information (which is useful for troubleshooting the cause of a crash). C++ profilers will also no longer be able to display function names (this does not affect the built-in GDScript profiler).

注解

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

针对大小而不是速度优化

  • Space savings: High

  • Difficulty: Easy

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

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

scons p=windows target=release tools=no optimize=size

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

禁用 3D

  • Space savings: Moderate

  • Difficulty: Easy

  • Performed in official builds: No

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

scons p=windows target=release tools=no disable_3d=yes

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

禁用高级 GUI 节点

  • Space savings: Moderate

  • Difficulty: Easy

  • Performed in official builds: No

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

scons p=windows target=release tools=no disable_advanced_gui=yes

禁用不需要的模块

  • Space savings: Very low to moderate depending on modules

  • Difficulty: Medium to hard depending on modules

  • Performed in official builds: No

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

scons --help

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

scons p=windows target=release tools=no module_arkit_enabled=no module_assimp_enabled=no module_bmp_enabled=no module_bullet_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_etc_enabled=no module_gdnative_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_jsonrpc_enabled=no module_mbedtls_enabled=no module_mobile_vr_enabled=no module_opensimplex_enabled=no module_opus_enabled=no module_pvr_enabled=no module_recast_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_webm_enabled=no module_webrtc_enabled=no module_websocket_enabled=no module_xatlas_unwrap_enabled=no

如果这被证明不适合你的用例, 你应该回顾一下模块列表, 看看哪些模块是你的游戏仍然需要的(例如, 你可能想保留网络相关的模块,regex支持, 或者播放视频的theora/webm).

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

# custom.py

module_arkit_enabled = "no"
module_assimp_enabled = "no"
module_bmp_enabled = "no"
module_bullet_enabled = "no"
module_camera_enabled = "no"
module_csg_enabled = "no"
module_dds_enabled = "no"
module_enet_enabled = "no"
module_etc_enabled = "no"
module_gdnative_enabled = "no"
module_gridmap_enabled = "no"
module_hdr_enabled = "no"
module_jsonrpc_enabled = "no"
module_mbedtls_enabled = "no"
module_mobile_vr_enabled = "no"
module_opensimplex_enabled = "no"
module_opus_enabled = "no"
module_pvr_enabled = "no"
module_recast_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_webm_enabled = "no"
module_webrtc_enabled = "no"
module_websocket_enabled = "no"
module_xatlas_unwrap_enabled = "no"