Version: Unity 6.0 (6000.0)
언어 : 한국어
웹 빌드를 위한 Nginx 서버 구성 설정
Windows

웹 빌드용 Node.js 서버 구성 설정

Node.js 서버가 Unity 웹 빌드에서 작동하도록 해주는 Node.js 서버 설정 파일을 생성합니다.

Node.js는 서버 측에서 JavaScript 코드를 실행할 수 있게 해주는 오픈 소스 런타임 환경입니다. Unity에서 Node.js 앱을 사용하여 Unity 웹 빌드를 제공할 수 있습니다.

Node.js와 설치 및 사용 방법에 대한 상세 내용은 Node.js 기술 자료를 참조하십시오.

package.json 파일 설정

Unity Web과 함께 사용할 package.json 파일을 생성하는 법은 다음과 같습니다.

  1. 텍스트 에디터 또는 IDE에서 파일을 생성합니다.

  2. 파일 이름을 package.json으로 지정합니다.

  3. 파일을 Node.js 프로젝트 디렉토리에 저장합니다.

  4. 다음 코드를 복사해 파일에 붙여 넣습니다.

    {
      "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과 함께 사용할 index.js 파일을 준비하는 방법은 다음과 같습니다.

  1. 텍스트 에디터 또는 IDE에서 파일을 생성합니다.

  2. 파일 이름을 package.json으로 지정합니다.

  3. 파일을 Node.js 프로젝트 디렉토리에 저장합니다.

  4. 다음 코드를 복사해 파일에 붙여 넣습니다.

    #!/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();
    });
    
  5. 빌드가 CORS를 통해 로드되는 경우(예: 다른 웹사이트에 내장됨), 아래의 코드 줄을 true로 설정하고, 그렇지 않을 경우에는 false로 설정합니다.

    const enableCORS = true; 
    
  6. 멀티스레딩 지원으로 빌드를 만든 경우 코드의 다음 줄을 true로 설정하고, 그렇지 않으면 false로 설정합니다.

    const enableWasmMultithreading = true;
    

이제 서버 설정을 Unity 웹 빌드와 함께 사용할 수 있습니다.

추가 리소스

웹 빌드를 위한 Nginx 서버 구성 설정
Windows