如何为 Unity WebGL Build 配置 Node Express 服务器

How to configure Node Express server for Unity WebGL Build

提问人:Lee Probert 提问时间:11/7/2023 更新时间:11/7/2023 访问量:57

问:

Unity 使用 Brotli 压缩和 Web 汇编文件发布其 WebGL 构建。我注意到,如果您使用生产版本,则文件被命名并且 - 这似乎是压缩的 Brotli 压缩文件。public.wasm.unitywebpublic.data.unityweb

这是我用于添加标头和内容类型的节点快速代码:

const app = express();
app.use(express.static(path.join(__dirname, 'public'), {
    setHeaders: function(res, path) {

        if(path.endsWith(".unityweb") || path.endsWith(".br")){
            res.set("Content-Encoding", "br");
        }else if(path.endsWith(".gzip")){
            res.set("Content-Encoding", "gzip");
        }
        
        if(path.endsWith(".wasm")){
            res.set("Content-Type", "application/wasm");
        }

        if(path.endsWith(".data")){
            res.set("Content-Type", "application/octet-stream");
        }
    }
}));

我在浏览器中收到来自Unity的警告,指出:

public.loader.js:17 WebAssembly streaming compilation failed! This can happen for example if "Content-Encoding" HTTP header is incorrectly enabled on the server for file Build/public.wasm, but the file is not pre-compressed on disk (or vice versa). Check the Network tab in browser Devtools to debug server header configuration.

网络选项卡显示错误和响应标头,如下所示:enter image description here

任何人都可以帮我为这些文件配置服务器吗?我在 Google App Engine 上托管它。

节点 .js Express unity-game-engine 谷歌应用引擎 unity-webgl

评论

1赞 BugFinder 11/7/2023
正确。您只需设置 unityweb 和 gzip 的编码。请注意,当然,brotti 不是唯一的选择,文件名不一定会更改
0赞 Lee Probert 11/7/2023
那么我错过了什么呢?...这些都失败了public.datapublic.wasm
1赞 BugFinder 11/7/2023
正如它所说的那样。您尚未设置编码
0赞 Lee Probert 11/7/2023
那么文件的内容类型和压缩格式是一样的吗?res.set("Content-Encoding", "br");
1赞 BugFinder 11/7/2023
如果您阅读 Web 设置,它会识别出需要附加的所有文件类型(为什么它们不能像世界其他地方一样只使用文件扩展名)

答: 暂无答案