为尺寸优化构建

解释

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

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

参见

You can use the online Godot build options generator to generate a custom.py file containing SCons options. You can then save this file and place it at the root of your Godot source directory.

禁用3D

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

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

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

禁用高级GUI节点

Most small games don’t require complex GUI controls such as Tree, ItemList, TextEdit or GraphEdit. They can be disabled using a build flag:

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

禁用不需要的模块

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

  1. scons --help

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

  1. 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_webp_enabled=no module_webrtc_enabled=no module_websocket_enabled=no module_xatlas_unwrap_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, or theora/webm to play videos).

Alternatively, you can supply a list of disabled modules by creating custom.py at the root of the source, with the contents similar to the following:

  1. # custom.py
  2. module_arkit_enabled = "no"
  3. module_assimp_enabled = "no"
  4. module_bmp_enabled = "no"
  5. module_bullet_enabled = "no"
  6. module_camera_enabled = "no"
  7. module_csg_enabled = "no"
  8. module_dds_enabled = "no"
  9. module_enet_enabled = "no"
  10. module_etc_enabled = "no"
  11. module_gdnative_enabled = "no"
  12. module_gridmap_enabled = "no"
  13. module_hdr_enabled = "no"
  14. module_jsonrpc_enabled = "no"
  15. module_mbedtls_enabled = "no"
  16. module_mobile_vr_enabled = "no"
  17. module_opensimplex_enabled = "no"
  18. module_opus_enabled = "no"
  19. module_pvr_enabled = "no"
  20. module_recast_enabled = "no"
  21. module_regex_enabled = "no"
  22. module_squish_enabled = "no"
  23. module_svg_enabled = "no"
  24. module_tga_enabled = "no"
  25. module_theora_enabled = "no"
  26. module_tinyexr_enabled = "no"
  27. module_upnp_enabled = "no"
  28. module_vhacd_enabled = "no"
  29. module_vorbis_enabled = "no"
  30. module_webm_enabled = "no"
  31. module_webp_enabled = "no"
  32. module_webrtc_enabled = "no"
  33. module_websocket_enabled = "no"
  34. module_xatlas_unwrap_enabled = "no"

参见

Overriding the build options.

针对大小而不是速度优化

Godot 3.1 onwards allows compiling using size optimizations (instead of speed). To enable this, set the optimize flag to size:

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

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

编译时使用连接时间优化

启用链接时优化可以在性能和文件大小方面生成更高效的二进制文件。它通过消除重复的模板功能和未使用的代码来工作。目前,它可以与GCC和MSVC编译器一起使用:

  1. scons p=windows target=release tools=no use_lto=yes

使用此选项,链接变得慢得多,因此它应该仅用于发布版本。

剥离二进制文件

如果从源代码构建,请记住从二进制文件中剥离调试符号:

  1. strip godot.64