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
。
這樣可以將編譯後二進位檔案減少到原先五分之一到十分之一的大小。缺點是當機追蹤中就無法再提供準確的資訊了(可用於搜尋當機原因)。:ref:`C++ 性能分析器 <doc_using_cpp_profilers>`也將無法顯示函式名稱(不影響內建的 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 可用記憶體才能成功連結。
最佳化大小而非速度¶
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
必須要禁用 Tools 才能使用該旗標,因為編輯器並非設計給不支援 3D 的情景。關閉 3D 支援後,二進位檔的大小約可以減少 15%。
禁用進階 GUI 節點¶
大多數小型遊戲都不需要如 Tree, ItemList, TextEdit 或 GraphEdit 等的複雜 GUI 控制。可以通過一個建置旗標來禁用:
scons target=template_release disable_advanced_gui=yes
以下所有東西都會被禁用:
檔案對話框
PopupMenu
樹
文字編輯器
文字編輯器
SyntaxHighlighter
CodeHighlighter
TreeItem
最佳化
SpinBox
顏色
ColorPickerButton
RichTextlabel
RichTextEffect
清除變換
檔案對話框
注意事項
建立內容
建立內容
建立內容
建立內容
建立內容
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.