包含此页的版本:
不含此页的版本:
创建一个Node.js服务器配置文件,让您的Node.js服务器与您的 Unity Web 版本配合使用。
Node.js 是一个开源运行时环境,可让您在服务器端执行 JavaScript 代码。在 Unity 中,您可以使用 Node.js 应用程序来提供 Unity Web 版本。
有关Node.js以及如何安装和使用它的更多信息,请参阅Node.js文档。
要创建可与 Unity Web 一起使用的package.json文件,请执行以下作:
在文本编辑器或 IDE 中创建文件。
将文件命名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.jsfile 作为服务器项目的入口点,并定义项目将依赖的库。
要准备 index.js 文件以用于 Unity Web,请执行以下作:
在文本编辑器或 IDE 中创建文件。
将文件命名index.js.
将文件保存在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 构建一起使用。