部署Unity的WebGL到CentOS服务器

Unity越来越好了,新建项目切换到WebGL目标平台后使用缺省设置编译出的文件就可以用于服务器部署了(https://docs.unity.cn/cn/current/Manual/webgl-building.html).这次我使用了Unity 2021.3.2f1 macOS版,编译后会有如下结构的文件:

可以看到有*.br后缀的文件.这是因为在编译WebGL时默认开启了压缩选项,后缀为br的为Brotli压缩方法,如果是gz后缀是gzip压缩方法.上传至服务器,访问出现了如下提示:

Unable to parse Build/*.framework.js.br! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header “Content-Encoding: br” present. Check browser Console and Devtools Network tab to debug.

这是因为服务器没有正确设置响应标头来处理压缩格式,通过直接访问*.br文件我们可以看到如下结果,说明浏览器不能正确的解压缩文件:

有多种解决方式,比如在Unity的编译设置中禁用压缩选项等等,禁用压缩可以参考官方文档:https://docs.unity.cn/cn/current/Manual/webgl-deploying.html.

我的服务器是CentOS系统,需要到Apache的配置文件路径下创建对br等文件类型映射,让浏览器能正确解析该后缀类型.默认在etc/httpd/conf.d/文件夹下我们创建一个新的*.conf文件,如 vim forUnityWeb.conf,内容如下:

<IfModule mod_mime.c>
	RemoveType .br
	RemoveLanguage .br
	AddEncoding br .br
	AddType application/octet-stream .data.br
	AddType application/wasm .wasm.br
	AddType application/javascript .js.br
	AddType application/octet-stream .symbols.json.br
</IfModule>

保存该文件,有关其它服务器配置可以查看官方文档(https://docs.unity.cn/cn/current/Manual/webgl-server-configuration-code-samples.html),做好配置后再使用systemctl reload httpd命令重载Apache配置,然后再次访问br文件可以看到浏览器可以正常解压这个文件了:

清空浏览器缓存再次访问WebGL页面就没有问题了,好了,今天就写到这里,回见.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注