工作记录-WASM streaming instantiate以及删除引擎默认资源
2019-02-27 12:30:00在较老版本的 Unity 上使用 WASM streaming instantiate
背景
项目用的是 Unity 2018.2.12f1,这个版本使用的 emscripten 是 1.37.33,Unity 在这个版本的 WebGL Support 中有 ItegrateWasm.js 这样一个源文件,用来覆盖 emscripten 的 wasm 默认加载方式。文件头部有如下注释
// The following code overrides the default Emscripten instantiation of the WebAssembly module
// as a workaround for incorrectly initialized Runtime object properties
// (module export is not available at launch time if the module is compiled asynchronously).
// This file should be removed as soon as this bug is fixed in Emscripten.
我理解的意思是如果使用 WASM streaming instantiate 来 asynchronously compile,会带来 bug。具体是什么 bug 我没有验证。
Unity 2019.1.0b2 版本中更新 emscripten,去掉了 ItegrateWasm.js,也就是说支持了使用 emscripten 的默认方式:不提前加载 wasm 文件放到 Moduel['wasmBinary'],通过实现 Module['locateFile'] 提供URL,让 emscripten 调用 WebAssembly.instantiateStreaming。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
处理方法
- 确定
Unity安装目录,这里我们用/path/to/unity代替 - 删除
/path/to/unity/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/prejs/IntegrateWasm.js - 正常编译发布
WebGL平台 - 修改
UnityLoader.js(加入Module['locateFile']函数,参考Unity 2019.1.0b2)
var unityInstance = {
// ...
Module: {
// ...
locateFile: function(url) {
return "Build/".concat(url == "build.wasm" ? this.wasmCodeUrl : url);
},
},
// ...
};
- 把
xxxx.wasm.code.unityweb文件重命名为xxxx.wasm - 在
xxxx.json把xxxx.wasm.code.unityweb修改为xxxx.wasm nginx的mime.types配置文件新加一行(让浏览器mime类型判断通过)
application/wasm wasm;
nginx的nginx.conf配置文件在正确位置加入下面的配置(让浏览器来解压)
location ~ .+\.(unityweb|wasm)$ {
add_header 'Content-Encoding' 'gzip';
}
去掉游戏发布后包体中 Unity 引擎默认资源
背景
Unity 引擎有许多自带资源,分为以下三类
unity default resources:一定会发布到包体中unity_builtin_extra:根据使用情况,会进行裁剪unity editor resources:编辑器使用
项目因为对于首包大小十分敏感,并且有一些 unity default resources 中的资源并没有使用,所以希望裁剪 unity default resources 中的资源。这里另外提一句,unity default resources 这个包里面有 Unity 自己的 Splash Texture,还有各种版本的 logo。其实这部分资源,大部分游戏都不会用到。
处理方法
-
确定
Unity安装目录,这里我们用/path/to/unity代替 -
把
Runtime.7z解压到Unity安装目录同级目录,这里我们用/path/to/unity_parent代替Runtime.7z属于源码的一部分,这里就不提供了,有需要的可以网上找Unity 4.3的代码,或者自己用 AssetStudio 解包导出 -
在
/path/to/unity_parent目录下新建文件夹,名称EditorUnity源码中有路径判断,所以Runtime和Editor一定要存在并且与安装目录同级(默认是C:\Program Files) -
运行命令
"/path/to/unity/Editor/Unity.exe" -cleanedLogFile "/path/to/log/clean_builtin_resources.log" -logFile "/path/to/log_folder/builtin_resources.log" -batchmode -forceFullStacktrace Assert -forgetProjectPath -nographics -force-gfx-direct -projectpath ”/path/to/unity_parent/Runtime/Resources“ -buildBuiltinUnityResources -outputpath ”/path/to/output“ -quit
命令中的 `/path/to/log` 目录是临时log保存的目录,可以自定义。
命令中的 `/path/to/output` 目录是 `asset bundle` 输出的目录,可以自定义。
注意目录一定要保证存在并且正确。
-
用
Unity打开/path/to/unity_parent/Runtime/Resources工程,删除Assets目录下不用的资源 一定要先运行batch命令,让Unity处理好资源之后再打开工程,主要是Arial.ttf这个字体文件的材质的Shader路径的问题。另外删除资源不能删除Arial.ttf和与其配套的Font.shader,因为引擎代码中有判断。 -
用命令重新打包
-
把
/path/to/output目录下的asset bundle拷贝覆盖掉对应平台的unity_default_resource文件例如
/path/to/output/resources_webgl覆盖/path/to/unity/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/data/unity_default_resource
Update
Runtime文件夹在编译完Asset Bundle之后要重命名,否则会导致Unity Editor打开找不到GUI shader,整个编辑器显示粉色
Unity 有一些“规则”写死在代码里,真的很无奈