웹 빌드용 Node.js 서버 구성을 설정합니다.
웹 빌드를 제공하기 위해 Node.js 서버를 설정하는 방법에 대한 정보입니다.
읽는 시간 3분최근 업데이트: 10일 전
Node.js 서버가 Unity 웹 빌드와 함께 작동하도록 Node.js 서버 구성 파일을 생성합니다.
Node.js는 서버 측에서 JavaScript 코드를 실행할 수 있는 오픈 소스 런타임 환경입니다. Unity Node.js 앱을 사용하여 Unity 웹 빌드를 지원할 수 있습니다.
Node.js 및 설치 및 사용 방법에 대한 자세한 내용은 Node.js 문서를 참조하십시오.
package.json 파일 설정
Unity Web에서 사용할 수 있는 package.json 파일을 만들려면 다음 단계를 따르십시오.
-
텍스트 에디터 또는 IDE에서 파일을 생성합니다.
-
파일의 이름을 지정합니다.
package.json -
파일을 Node.js 프로젝트 디렉토리에 저장합니다.
-
다음 코드를 파일에 붙여넣습니다.{ "name": "node_server_example", "version": "1.0.0", "description": "Node.js로 Unity 웹 빌드를 제공하는 예시", "main": "index.js", "dependencies": { "express": "^4.19.2" }}
이제 파일을 사용할 준비가 되었습니다. 이 코드는 파일을 서버 프로젝트의 엔트리 포인트로 정의하고 프로젝트가 의존할 라이브러리를 정의합니다.
package.jsonindex.jsindex.js 파일 설정
Unity Web에서 사용할 index.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 웹 빌드와 함께 사용할 수 있습니다.