为尺寸优化构建

解释

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

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

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

剥离二进制文件

  • 节省空间:非常高

  • 难度:简单

  • 在官方构建中执行:

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

strip path/to/godot.binary

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

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

备注

上面的命令无法用于使用 MSVC 编译的 Windows 二进制文件,Android 和 HTML5 等平台也一样。作为替代,请在编译时向 SCons 命令行传递 debug_symbols=no

针对大小而不是速度优化

  • 节省空间:

  • 难度:简单

  • 在官方构建中执行:是,但仅限于 HTML5

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

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

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

禁用 3D

  • 节省空间:一般

  • 难度:简单

  • 在官方构建中执行:

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

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

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

禁用高级 GUI 对象

  • 节省空间:一般

  • 难度:简单

  • 在官方构建中执行:

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

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

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

  • FileDialog

  • PopupMenu

  • Tree

  • TextEdit

  • TreeItem

  • OptionButton

  • SpinBox

  • ColorPicker

  • ColorPickerButton

  • RichTextLabel

  • RichTextEffect

  • CharFXTransform

  • PopupDialog

  • WindowDialog

  • AcceptDialog

  • ConfirmationDialog

  • MarginContainer

  • ViewportContainer

  • SplitContainer

  • HSplitContainer

  • GraphNode

  • GraphEdit

禁用不需要的模块

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

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

  • 在官方构建中执行:

许多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"