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.

最佳化建置檔案大小

說明

有時候希望以檔案大小為優先進行最佳化,而非速度。這意味著不會編譯引擎中未使用的函式,並利用特定的編譯器旗標來協助縮小建置檔案。常見情境包含針對行動裝置與 Web 平台建立建置檔。

本教學旨在概述各種縮小二進位檔的方法。在繼續之前,建議先閱讀針對各平台編譯 Godot 的相關教學。

以下選項依照重要程度(節省空間最多到最少)的順序排列。

移除二進位檔符號

  • 節省空間: 非常高

  • 難度: 簡單

  • 官方建構是否執行:

如果你從原始碼建構 Windows(MinGW)、Linux 或 macOS 的二進位檔,請記得透過發行版安裝 strip 套件,然後執行下列指令以移除除錯符號:

strip path/to/godot.binary

在 Windows 上,大多數 MinGW 工具鏈都已內含 strip.exe

這樣做能將編譯後的二進位檔案縮小至原本的五分之一到十分之一。缺點是當機回溯將無法再提供準確資訊(此對於排除當機原因很有幫助)。C++ 性能分析工具 也無法顯示函式名稱(但不影響內建的 GDScript 性能分析器)。

備註

上述指令無法用於以 MSVC 編譯的 Windows 二進位檔,以及 Android 和 Web 等平台。請改為在 SCons 編譯指令列加上 debug_symbols=no

優先最佳化大小而非速度

  • 節省空間:

  • 難度: 簡單

  • 官方建構是否執行: 是,但僅限於 Web 平台

It is possible to compile Godot using size optimizations (instead of speed). To enable this, set the optimize flag to size:

scons target=template_release optimize=size

某些平台(如 WebAssembly)預設即使用此模式。

Godot 4.5 新增了 size_extra 選項,可進一步縮小體積。

scons target=template_release optimize=size_extra

偵測當前專案所用功能並停用未使用的功能

  • 節省空間: 中等到高,視專案而定

  • 難度: 簡單至中等,視專案而定

  • 在官方建構中執行:

Godot 提供一個 使用引擎編譯配置編輯器 工具,可偵測目前專案使用到的功能並建立建置設定檔。儲存後,即可在編譯自訂匯出樣板時將該設定檔傳給 SCons:

scons target=template_release build_profile=/path/to/profile.gdbuild

請注意,對某些專案而言,功能偵測可能過於積極,導致停用了執行時其實需要的功能。例如某些功能的使用方式無法靜態偵測(像是於執行時動態產生並執行的腳本)。

你也可以依下列章節停用更細部的功能,但請記得其中多數功能可由引擎的編譯組態偵測器自動判斷。

停用進階文字伺服器

  • 節省空間:

  • 難度: 簡單

  • 在官方建構中執行:

Godot 預設使用進階文字伺服器,支援以下功能:

  • 支援由右至左排版與複雜文字腳本,這對阿拉伯文、希伯來文等語言是必要的。

  • 字體連字與 OpenType 功能(如小型大寫字母、分數、斜線零)。

Godot 也提供一個預設不會編譯的備用文字伺服器。這個備用文字伺服器可作為輕量級選擇,取代進階文字伺服器:

scons target=template_release module_text_server_adv_enabled=no module_text_server_fb_enabled=yes

若你的專案僅需支援拉丁文、希臘文與西里爾文等語系,備用文字伺服器就已足夠。

此外,備用文字伺服器處理大量文字的速度也比進階文字伺服器快,因此很適合用於行動裝置或 Web 專案。

備註

使用 module_text_server_adv_enabled=no 時,請務必同時加上 module_text_server_fb_enabled=yes,否則編譯出來的二進位檔將不包含任何文字伺服器,專案執行時將無法顯示任何文字。

停用 3D 支援

  • 節省空間: 中等

  • 難度: 簡單

  • 在官方建構中執行:

對於 2D 遊戲而言,完整的 3D 引擎通常不必要,因此可使用建置旗標將其停用:

scons target=template_release disable_3d=yes

必須同時停用 Tools 功能才能啟用此旗標,因為 Godot 編輯器設計上必須支援 3D。停用 3D 支援後,二進位檔大小約可減少 15%。

停用進階 GUI 元件

  • 節省空間: 中等

  • 難度: 簡單

  • 在官方建構中執行:

大多數小型遊戲不需要如 Tree、ItemList、TextEdit 或 GraphEdit 等複雜 GUI 控制元件。可使用建置旗標將這些元件停用:

scons target=template_release disable_advanced_gui=yes

停用後將無法使用下列元件:

停用物理引擎

  • 節省空間: 低到中等

  • 難度: 簡單

  • 在官方建構中執行:

如果你的 3D 專案使用 Jolt Physics,就可以在編譯時停用 GodotPhysics3D,因為它不會被使用:

scons target=template_release module_godot_physics_3d_enabled=no

反之,若你的 3D 專案使用 GodotPhysics3D,就可以在編譯時停用 Jolt Physics:

scons target=template_release module_jolt_enabled=no

如果你的專案使用 3D 繪製但不使用物理(或使用 2D 繪製但不使用物理),也可以完全停用 2D 或 3D 物理。多數 3D 專案都能利用這點,因為它們通常不會用到 2D 物理:

scons target=template_release disable_physics_2d=yes
scons target=template_release disable_physics_3d=yes

停用不需要的模組

  • 節省空間: 非常低到中等,視模組而定

  • 難度: 中等至困難,視模組而定

  • 在官方建構中執行:

Godot 許多功能都以模組形式提供。你可以用下列指令查看模組列表:

scons --help

會顯示所有可停用的模組以及建置選項。若你正在開發簡單的 2D 遊戲,這些模組多數都可直接停用:

scons target=template_release module_astcenc_enabled=no module_basis_universal_enabled=no module_bcdec_enabled=no module_bmp_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_etcpak_enabled=no module_fbx_enabled=no module_gltf_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_interactive_music_enabled=no module_jsonrpc_enabled=no module_ktx_enabled=no module_mbedtls_enabled=no module_meshoptimizer_enabled=no module_mp3_enabled=no module_mobile_vr_enabled=no module_msdfgen_enabled=no module_multiplayer_enabled=no module_noise_enabled=no module_navigation_2d_enabled=no module_navigation_3d_enabled=no module_ogg_enabled=no module_openxr_enabled=no module_raycast_enabled=no module_regex_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, mp3/ogg/vorbis to play music, or theora to play videos).

此外,你可以在原始碼根目錄建立 custom.py,以清單方式指定要停用的模組,其內容類似如下:

custom.py
module_astcenc_enabled = "no"
module_basis_universal_enabled = "no"
module_bcdec_enabled = "no"
module_bmp_enabled = "no"
module_camera_enabled = "no"
module_csg_enabled = "no"
module_dds_enabled = "no"
module_enet_enabled = "no"
module_etcpak_enabled = "no"
module_fbx_enabled = "no"
module_gltf_enabled = "no"
module_gridmap_enabled = "no"
module_hdr_enabled = "no"
module_interactive_music_enabled = "no"
module_jsonrpc_enabled = "no"
module_ktx_enabled = "no"
module_mbedtls_enabled = "no"
module_meshoptimizer_enabled = "no"
module_mp3_enabled = "no"
module_mobile_vr_enabled = "no"
module_msdfgen_enabled = "no"
module_multiplayer_enabled = "no"
module_noise_enabled = "no"
module_navigation_2d_enabled = "no"
module_navigation_3d_enabled = "no"
module_ogg_enabled = "no"
module_openxr_enabled = "no"
module_raycast_enabled = "no"
module_regex_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"

也參考

覆寫建置選項

最佳化專案散佈方式

桌上型電腦

備註

本節僅適用於檔案發佈於未自帶壓縮或封包機制的桌機平台。例如,你在 itch.io 或 GitHub Releases 上提供 ZIP 壓縮檔時,以下建議就很有幫助。

像 Steam 這類平台已自動進行壓縮,因此不需要再自行打包 ZIP 檔案。

此外,你也可以針對專案分發本身進行最佳化。即使不重新編譯匯出範本,也能採取這些措施。

7-Zip 可用來建立壓縮效率更高的 ZIP 檔案,且與所有 ZIP 解壓縮工具(包含 Windows 內建的)相容。對大型專案來說,與一般 ZIP 工具相比,壓縮後可減少數十 MB,平均則能省下 1-5 MB 空間。建立這類 ZIP 檔所需時間較長,但解壓速度與普通 ZIP 檔無異。

若用 7-Zip 圖形介面,請選擇「Ultra」壓縮等級來建立 ZIP 檔。若用命令列,請執行下列指令:

7z a -mx9 my_project.zip folder_containing_executable_and_pck

Web

針對 Web 匯出檔案(特別是 .wasm.pck 等),開啟 gzip 或 Brotli 壓縮能大幅減少下載量,進而加快載入速度,尤其適用於慢速網路連線。

如果網頁伺服器能正確傳送預先壓縮好的 gzip 或 Brotli 檔案,建議直接產生高壓縮等級的這類檔案,效果會更好。若伺服器支援,Brotli 通常比 gzip 有更高壓縮比,建議優先選用。

詳情請參考 提供檔案服務