创建一个 Node.js 服务器配置文件,让 Node.js 服务器与 Unity Web 构建配合使用。
Node.js 是一个开源运行时环境,允许在服务器端执行 JavaScript 代码。在 Unity 中,可以使用 Node.js 应用程序为 Unity Web 构建服务。
有关 Node.js 及及安装和使用方法的更多信息,请参阅 Node.js 文档。
要创建可用于 Unity Web 的 package.json 文件,请执行以下操作:
在文本编辑器或集成开发环境中创建文件。
命名文件 package.json。
将文件保存在 Node.js 项目目录中。
将以下代码粘贴到文件中:
{
"name": "node_server_example",
"version": "1.0.0",
"description": "An example for serving a Unity Web build with Node.js",
"main": "index.js",
"dependencies": {
"express": "^4.19.2"
}
}
package.json 文件现在可以使用。该代码将 index.js 文件定义为服务器项目的入口点,并定义项目将依赖的库。
要准备 index.js 文件以用于 Unity Web,请执行以下操作:
在文本编辑器或集成开发环境中创建文件。
命名文件 package.json。
将文件保存在 Node.js 项目目录中。
将以下代码粘贴到文件中:
#!/usr/bin/env node
const path = require('path');
const express = require('express');
// Create express application
const app = express();
// Settings
const hostname = 'localhost';
const port = 8080;
const enableCORS = true;
const enableWasmMultithreading = true;
// Serve the current working directory
const unityBuildPath = __dirname; // Note: this makes the current working directory visible to all computers over the network.
app.use((req, res, next) => {
var path = req.url;
// Provide COOP, COEP and CORP headers for SharedArrayBuffer
// multithreading: https://web.dev/coop-coep/
if (enableWasmMultithreading &&
(
path == '/' ||
path.includes('.js') ||
path.includes('.html') ||
path.includes('.htm')
)
) {
res.set('Cross-Origin-Opener-Policy', 'same-origin');
res.set('Cross-Origin-Embedder-Policy', 'require-corp');
res.set('Cross-Origin-Resource-Policy', 'cross-origin');
}
// Set CORS headers
if (enableCORS) {
res.set('Access-Control-Allow-Origin', '*');
}
// Set content encoding depending on compression
if (path.endsWith('.br')) {
res.set('Content-Encoding', 'br');
} else if (path.endsWith('.gz')) {
res.set('Content-Encoding', 'gzip');
}
// Explicitly set content type. Files can have wrong content type if build uses compression.
if (path.includes('.wasm')) {
res.set('Content-Type', 'application/wasm');
} else if (path.includes('.js')) {
res.set('Content-Type', 'application/javascript');
} else if (path.includes('.json')) {
res.set('Content-Type', 'application/json');
} else if (
path.includes('.data') ||
path.includes('.bundle') ||
path.endsWith('.unityweb')
) {
res.set('Content-Type', 'application/octet-stream');
}
// Ignore cache-control: no-cache
// when if-modified-since or if-none-match is set
// because Unity Loader will cache and revalidate manually
if (req.headers['cache-control'] == 'no-cache' &&
(
req.headers['if-modified-since'] ||
req.headers['if-none-match']
)
) {
delete req.headers['cache-control'];
}
next();
});
app.use('/', express.static(unityBuildPath, { immutable: true }));
const server = app.listen(port, hostname, () => {
console.log(`Web server serving directory ${unityBuildPath} at http://${hostname}:${port}`);
});
server.addListener('error', (error) => {
console.error(error);
});
server.addListener('close', () => {
console.log('Server stopped.');
process.exit();
});
如果您的构建通过 CORS 加载(例如,嵌入到不同的网站上),请在代码中将以下行设置为 true,否则将其设置为 false:
const enableCORS = true;
如果创建的构建支持多线程,请在代码中将以下行设置为 true,否则将其设置为 false:
const enableWasmMultithreading = true;
您的服务器配置现已准备好用于 Unity Web 构建。