Up to date

This page is up to date for Godot 4.3. If you still find outdated information, please open an issue.

為網頁平台進行編譯

也參考

這個頁面描述的是如何從源碼編譯 HTML5 編輯器和匯出範本二進位檔案。如果你要找的是匯出專案到 HTML5,請閱讀 為 Web 匯出

系統需求

要為網頁平台進行編譯,需滿足下列需求:

也參考

要獲取編譯所需的 Godot 源碼,請參閱 取得原始碼

有關配合 Godot 使用 SCons 的一般說明,請參考 建置系統簡介

備註

Emscripten 3.1.39+ is recommended, but older 3.x versions are known to work.

Please note that the minimum requirement for GDExtension support is 3.1.14.

建置匯出樣板

在開始之前,確認 emcc 在你的 PATH 中是可用的。這通常是由 Emscripten SDK 配置的,例如在呼叫 emsdk activatesource ./emsdk_env.sh/emsdk_env.bat 時。

開啟終端機,並移至 Godot 原始碼的根目錄。接著使用 SCons 來為 JavaScript 平台建置。將如果要建置正式版本,請將 target 指定為 release ,若要建置除錯版本,請將 target 指定為 release_debug

scons platform=web target=template_release
scons platform=web target=template_debug

By default, the JavaScriptBridge singleton will be built into the engine. Official export templates also have the JavaScript singleton enabled. Since eval() calls can be a security concern, the javascript_eval option can be used to build without the singleton:

scons platform=web target=template_release javascript_eval=no
scons platform=web target=template_debug javascript_eval=no

By default, WebWorker threads support is enabled. To disable it and only use a single thread, the threads option can be used to build the web template without threads support:

scons platform=web target=template_release threads=no
scons platform=web target=template_debug threads=no

The engine will now be compiled to WebAssembly by Emscripten. Once finished, the resulting file will be placed in the bin subdirectory. Its name is godot.web.template_release.wasm32.zip for release or godot.web.template_debug.wasm32.zip for debug.

最後,將用於正式樣板 (release) 的 ZIP 檔重新命名為 webassembly_release.zip

mv bin/godot.web.template_release.wasm32.zip bin/web_release.zip

並將用於除錯樣板 (debug) 的 ZIP 檔重新命名為 webassembly_release.zip

mv bin/godot.web.template_debug.wasm32.zip bin/web_debug.zip

GDExtension

由於性能和相容性的原因,預設的匯出範本不包括執行緒和 GDNative 支援。更多資訊見:ref:匯出頁面 <doc_javascript_export_options>

你可以使用選項 threads_enabled=yesgdnative_enabled=yes 建構匯出範本,以啟用執行緒或 GDNative 支援:

scons platform=web dlink_enabled=yes target=template_release
scons platform=web dlink_enabled=yes target=template_debug

產生的二進位檔會放在 bin/ 子資料夾中。通常來說會依照下列命名慣例:

最後,將用於正式樣板 (release) 的 ZIP 檔重新命名為 webassembly_release.zip

mv bin/godot.web.template_release.wasm32.dlink.zip bin/web_dlink_release.zip
mv bin/godot.web.template_debug.wasm32.dlink.zip bin/web_dlink_debug.zip

內建主題編輯器。

也可以建立一個可以在瀏覽器中運作的 Godot 編輯器版本。編輯器版本需要執行緒支援,不推薦使用本地建構。你可以用以下方法建構編輯器:

scons platform=web target=editor

Once finished, the resulting file will be placed in the bin subdirectory. Its name will be godot.web.editor.wasm32.zip. You can upload the zip content to your web server and visit it with your browser to use the editor.

關於網路伺服器的要求,請參考:ref:匯出頁面 <doc_javascript_export_options>

小訣竅

Godot 儲存庫包含一個「託管本機 Web 伺服器的 Python 腳本 <https://raw.githubusercontent.com/godotengine/godot/master/platform/web/serve.py>」。這可用於在本地測試網頁編輯器。

編譯編輯器後,提取在「bin/」資料夾中建立的 ZIP 存檔,然後在 Godot 儲存庫根目錄中執行以下命令:

# You may need to replace `python` with `python3` on some platforms.
python platform/web/serve.py

This will serve the contents of the bin/ folder and open the default web browser automatically. In the page that opens, access godot.editor.html and you should be able to test the web editor this way.

請注意,對於生產用例,不應使用此基於 Python 的 Web 伺服器。相反,您應該使用已建立的 Web 伺服器,例如 Apache 或 nginx。