ドキュメント

Unity Engine

Unity Engine

ウェブ ビルド用の 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 ファイルを作成するには、以下を行います。
  1. テキストエディターまたは IDE でファイルを作成します。
  2. ファイル
    package.json
    に名前を付けます。
  3. Node.js プロジェクトディレクトリにファイルを保存します。
  4. 以下のコードをファイルに貼り付けます。
    { "name": "node_server_example", "version": "25270591" "description": "Node.js で Unity ウェブ ビルドを提供する例", "main": "index.js", "dependencies": { "express": "^4.19.2" }}
これで、
package.json
ファイルを使用する準備ができました。コードは、
index.js
ファイルをサーバープロジェクトのエントリーポイントとして定義し、プロジェクトが依存するライブラリを定義します。

インデックス js ファイルの設定

Unity ウェブ で使用する インデックス.js ファイルを準備するには、以下を行います。
  1. テキストエディターまたは IDE でファイルを作成します。
  2. ファイル
    index.js
    に名前を付けます。
  3. Node.js プロジェクトディレクトリにファイルを保存します。
  4. 以下のコードをファイルに貼り付けます。
    #!/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();});
  5. オリジン間リソース共有 (CORS) を使用してビルドをロードする場合 (例えば、異なるウェブサイトに埋め込まれた場合)、コードの以下の行を
    true
    に設定します。それ以外の場合は、
    false
    に設定します。
    const enableCORS = true;
  6. マルチスレッドサポートを使用してビルドを作成した場合は、コードの以下の行を
    true
    に設定します。それ以外の場合は、
    false
    に設定します。
    const enableWasmMultithreading = true;
これで、サーバー設定が Unity ウェブビルドで使用できるようになりました。

追加リソース