WebGL でネットワークを利用するには、以下の 2 つの方法があります。
任意で、JavaScript の WebSockets または WebRTC を使用して、独自のネットワークを実装することができます。JavaScript のコードはネットワーク接続を実装するための IP ソケットに直接アクセスできないため、.NET のネットワーククラスは使用できないことに注意してください。
Unity は、WebGL の UnityWebRequest クラスをサポートします。UnityWebRequest クラスを実装するために、Unity は JavaScript Fetch API を使用します。これはブラウザーを使用してウェブリクエストを処理します。これにより、クロスドメインリソースへのアクセスにセキュリティ制限が課されます。
Unity コンテンツをホストするサーバー以外のサーバーにウェブリクエストを送信する場合は、送信先のサーバーが Unity コンテンツを承認する必要があります。
WebGL でクロスドメインのウェブリソースにアクセスするには、アクセスしようとしているサーバーが オリジン間リソース共有 (CORS) を使用してドメインのウェブリソースを認証する必要があります。
UnityWebRequest を使用してコンテンツにアクセスしようとするときに、リモートサーバーに CORS が設定されていないと、ブラウザーのコンソールに以下のようなエラーが表示されます。
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myserver.com/. この問題はリソースを同じドメインに移すか、 CORS を使用可能にすると修正されます
サーバーは、送信する http レスポンスに Access-Control ヘッダーを加えて、どのウェブページがウェブブラウザーからその情報を読み取る権限を持っているかを示す必要があります。
Access-Control ヘッダーを加えて、Unity WebGL が任意のオリジンからウェブサーバー上のリソースにアクセスできるようにする方法は、次の例を参照してください。この例では、一般的なリクエストヘッダーを含み、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 は非同期であるため、ブラウザーに制御を戻さないとダウンロードが終了しない可能性があります。代わりに、Coroutine と yield ステートメントを使用して、ダウンロードが終了するのを待ちます。詳細については、UnityWebRequest を使ったコルーチンの例 を参照してください。
Unity Multiplayer は WebSocket プロトコルによる通信を可能にします。NetworkServer.useWebSockets を参照してください。
WebGL では IP ソケットに直接アクセスすることはできませんが、WebSockets や WebRTC (ブラウザーでサポートされている最も一般的な 2 つのネットワークプロトコル) を使ってこれを回避できます。WebSockets は広くサポートされており、WebRTC はブラウザー間のピアツーピア接続と信頼性の低い接続を可能にします。Unity には、WebSockets や WebRTC を使用できるビルトインの API はありませんが、JavaScript プラグイン を使って実装することができます。WebSocket ネットワークを実装するプラグインは、Unity Asset Store で見つけることができます。
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.