다음 두 가지 방법으로 WebGL에서 네트워킹을 사용할 수 있습니다.
UnityWebRequest 클래스
Unity 멀티플레이어(구 UNet)
참고: UNet은 지원이 중단된 솔루션이며, 새로운 멀티플레이어 및 네트워킹 솔루션(MLAPI)이 개발 중입니다.
선택적으로 JavaScript의 WebSocket 또는 WebRTC를 사용하여 고유한 네트워킹을 구현할 수 있습니다. .NET 네트워킹 클래스는 사용할 수 없습니다. JavaScript 코드는 IP 소켓에 직접 액세스하여 네트워크 연결을 구현할 수 없기 때문입니다.
이 페이지에는 WebGL에서 네트워킹을 사용하기 위한 가이드라인이 포함되어 있습니다.
Unity는 WebGL에서 UnityWebRequest 클래스를 지원합니다. UnityWebRequest 클래스를 구현하기 위해 Unity는 브라우저를 사용하여 웹 요청을 처리하는 JavaScript Fetch API를 사용합니다. 이로 인해 크로스 도메인 리소스 액세스에 대한 보안 제한이 가해질 수 있습니다.
Unity 콘텐츠를 호스팅하는 서버가 아닌 다른 서버로 웹 요청을 전송하는 경우 전송 대상 서버에서 Unity 콘텐츠를 승인해야 합니다.
WebGL에서 크로스 도메인 웹 리소스에 액세스하려면 액세스 대상 서버가 CORS(Cross-Origin Resource Sharing)를 사용하여 크로스 도메인 웹 리소스를 승인해야 합니다.
UnityWebRequest를 사용하여 콘텐츠에 액세스하려고 하는 경우 원격 서버에 CORS가 올바르게 설정 또는 구성되어 있지 않으면 다음과 같은 오류가 브라우저 콘솔에 표시됩니다.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myserver.com/. This can be fixed by moving the resource to the same domain or enabling CORS.
서버는 전송하는 http 리스폰스에 Access-Control 헤더를 추가해야 합니다. 그래야만 어느 웹 페이지가 웹 브라우저에서 해당 정보를 읽을 권한이 있는지 나타낼 수 있습니다.
Unity WebGL이 모든 출처의 웹 서버에 있는 리소스에 액세스할 수 있도록 허용하는 Access-Control 헤더를 추가하는 방법에 대한 데모는 다음 예제를 참조하십시오. 이 예제는 공통 요청 헤더를 포함하며, GET, POST 또는 OPTIONS 메서드를 허용합니다.
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Origin": "*",
다음과 같이 UnityWebRequest 다운로드를 차단하는 코드는 사용하지 마십시오.
while(!www.isDone) {}
UnityWebRequest 다운로드가 완료될 때까지 기다리도록 스레드를 차단할 수 없습니다. 그렇게 하면 애플리케이션이 멈춥니다. WebGL은 단일 스레드이고 JavaScript의 fetch
API는 비동기식이므로, 브라우저에 컨트롤을 반환하지 않으면 다운로드가 완료되지 않을 수 있습니다. 대신에 코루틴 및 yield 문을 사용하여 다운로드가 완료될 때까지 기다리십시오. 자세한 내용은 UnityWebRequest를 사용하는 코루틴 예제를 참조하십시오.
Unity 멀티플레이어를 사용하면 WebSocket 프로토콜을 통한 통신이 가능합니다. NetworkServer.useWebSockets를 참조하십시오.
WebGL은 IP 소켓에 대한 직접 액세스를 허용하지 않지만, WebSocket 또는 WebRTC(브라우저에서 지원하는 가장 일반적인 두 가지 네트워킹 프로토콜)를 사용하여 이 문제를 해결할 수 있습니다. WebSocket은 널리 지원되는 반면, WebRTC는 브라우저와 신뢰할 수 없는 연결 간의 P2P 연결을 허용합니다. Unity에는 WebSocket 또는 WebRTC를 사용할 수 있는 빌트인 API가 없지만, JavaScript 플러그인을 사용하여 이 기능을 구현할 수 있습니다. Unity 에셋 스토어에서 WebSocket 네트워킹을 구현하는 플러그인을 찾을 수 있습니다.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.