1.需求
- linux系统(ubuntu20.04 LTS)
- Mono: 6.12.0.147源码.
- Emscripten: 1.39.9.
- godot源码
2.步骤
编译mono运行时和基础类库(Linux系统)
因为编译运行时使用的官方脚本只能使用Linux系统,所以我编译运行时使用的是Linux系统。
-
安装mono,参考https://www.mono-project.com/download/stable/#download-lin
sudo apt install gnupg ca-certificates sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list sudo apt update sudo apt install mono-devel
-
下载mono源码,并切换到分支mono-6.12.0.147, 可能需要科学上网,否则下载不全
git clone --recursive https://github.com/mono/mono.git git checkout mono-6.12.0.147
-
安装emscripten, 并在当前终端设置环境变量,参考https://emscripten.org/docs/getting_started/downloads.html
# Get the emsdk repo git clone https://github.com/emscripten-core/emsdk.git # Enter that directory cd emsdk # Fetch the latest version of the emsdk (not needed the first time you clone) git pull # Download and install the SDK tools. ./emsdk install 1.39.9 # Make the "1.39.9" SDK "active" for the current user. (writes .emscripten file) ./emsdk activate 1.39.9 # Activate PATH and other environment variables in the current terminal source ./emsdk_env.sh
-
下载godot官方用来编译mono运行时的脚本, 仅支持Linux系统
git clone https://github.com/godotengine/godot-mono-builds.git
-
运行脚本编译mono运行时和javascript平台的基础类库
#进入下载好的脚本目录 cd godot-mono-builds #MONO_SOURCE_ROOT为mono源码路径 export MONO_SOURCE_ROOT=$HOME/mono #在构建之前,需要将一些补丁应用于 Mono 源。 python3 ./patch_mono.py #在构建 Mono 之前,可能需要将一些补丁应用到 Emscripten SDK ./patch_emscripten.py #为 WebAssembly 构建运行时。 ./wasm.py configure --target=runtime ./wasm.py make --target=runtime #为 WebAssembly 编译基础类库BCL. ./bcl.py make --product=wasm
这个过程可能很慢如果没有科学上网,如果报错如下
执行以下命令安装缺失的库sudo apt-get install git autoconf libtool automake build-essential gettext cmake python3 curl
编译导出模板(Windows系统)
首先也要windows上安装mono,emscripten1.39.9,保证设置到环境变量
-
下载godot源码
git clone https://github.com/godotengine/godot.git
-
先生成胶水代码,否则会报错:Mono glue sources not found
进入godot源码根目录,执行以下命令# 构建临时二进制文件 scons p=windows tools=yes module_mono_enabled=yes mono_glue=no # 生成胶水代码 bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue #构建编辑器 scons p=windows target=release_debug tools=yes module_mono_enabled=yes
注意构建临时二进制文件时是"p=windows"。
前面说过编译运行时使用的Liunx系统,本来也想要使用Linux编译继续编译导出模板的。但是在这一步出现了错误,也就是使用如下命令构建胶水代码时出现了错误:scons: *** [thirdparty/oidn/weights/rtlightmap_hdr.gen.x11.opt.tools.64.o]…# Build temporary binary scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no # Generate glue sources bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue
原因不清楚,于是就改用windows平台生成胶水代码,后面把在Linux下生成的运行时wasm-runtime-release和bcl库复制了过来,然后继续在windows上编译导出模板。
-
编译javascript 平台导出模板,在godot源码根目录里执行
scons platform=javascript tools=no module_mono_enabled=yes mono_prefix="C:\Users\DannyJones\Desktop\wasm-runtime-release" bcl="C:\Users\DannyJones\Desktop\bcl"
mono_prefix和bcl分别是之前编译好的运行时目录和基础类库目录,注意一定要是绝对路径,否则会出现如下错误:找不到mono库
执行这条命令之前首先要保证环境变量里面有emsdk1.39.9。否则会出现javascript不是合法平台的错误。
此外,执行这条命令还可能出现 如下错误UnicodeDecodeError:‘gbk’ codec can’t decode …
解决办法:打开报错的文件subprocess.py,在以下位置添加红色框内的内容:
之后,重新执行以上命令,应该就能编译成功导出模板了。 -
要想使用导出模板还需要把bcl库复制到对应godot版本的模板库目录下面,一般为C:\Users\用户名\AppData\Roaming\Godot\templates
至此,就可以使用导出模板把项目导出为web了