ウェブ ビルド用の Node.js サーバー設定を設定する
ウェブ ビルドを提供するための Node.js サーバーの設定方法に関する情報。
読み終わるまでの所要時間 6 分最終更新 22日前
Node.js サーバー設定ファイルを作成し、Node.js サーバーで Unity ウェブビルドを操作できるようにします。
Node.js は、サーバーサイドで JavaScript コードを実行することができるオープンソースのランタイム環境です。Unity では、Node.js アプリを使用して Unity ウェブビルドを提供できます。
Node.js の詳細、およびインストールと使用方法については、Node.js ドキュメントを参照してください。
パッケージ.json ファイルを設定する
Unity ウェブ で使用する準備ができている パッケージ.json ファイルを作成するには、以下を行います。
-
テキストエディターまたは IDE でファイルを作成します。
-
ファイルに名前を付けます。
package.json -
Node.js プロジェクトディレクトリにファイルを保存します。
-
以下のコードをファイルに貼り付けます。{ "name": "node_server_example", "version": "25270591" "description": "Node.js で Unity ウェブ ビルドを提供する例", "main": "index.js", "dependencies": { "express": "^4.19.2" }}
これで、ファイルを使用する準備ができました。コードは、 ファイルをサーバープロジェクトのエントリーポイントとして定義し、プロジェクトが依存するライブラリを定義します。
package.jsonindex.jsインデックス js ファイルの設定
Unity ウェブ で使用する インデックス.js ファイルを準備するには、以下を行います。
-
テキストエディターまたは IDE でファイルを作成します。
-
ファイルに名前を付けます。
index.js -
Node.js プロジェクトディレクトリにファイルを保存します。
-
以下のコードをファイルに貼り付けます。#!/usr/bin/env nodeconst path = require('path');const express = require('express');// Create express applicationconst app = express();// Settingsconst 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.ビルドで圧縮を使用する場合、ファイルのコンテンツ タイプが正しくない場合があります。 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に設定します。falseconst enableCORS = true; -
マルチスレッドサポートを使用してビルドを作成した場合は、コードの以下の行をに設定します。それ以外の場合は、
trueに設定します。falseconst enableWasmMultithreading = true;
これで、サーバー設定が Unity ウェブビルドで使用できるようになりました。