ノート: UNet は非推奨となり、今後 Unity から削除される予定です。新しいシステムが開発中です。詳細は ブログ と FAQ を参照してください。 |
マルチプレイヤーゲームを作成するとき、ネットワーク化されたゲームオブジェクトのプロパティを同期すること に加え、大抵、ゲームのその他の情報を送信、受信、反応する必要があります。情報には、マッチの開始時間、プレイヤーがマッチに参加/離脱した時間、ゲームのタイプに特有な情報 - 例えば、CTF (Capture the Flag、旗取りゲーム) スタイルのゲームでは旗が取られたことをすべてのプレイヤーに知らせる、などがあります。
Unity ネットワーク高レベル API には、このような種類の情報を通信するための 3 つの主な方法があります。
遠隔手続きを使用すると、ネットワーク上のスクリプトのメソッドを呼び出すことができます。サーバーがすべてのクライアント上や特定のクライアント上でメソッドを呼び出しすることを可能にします。また、クライアントにサーバー上でメソッドの呼び出しをさせることも可能です。遠隔手続きを使って、ローカル (マルチプレイヤーゲームでない) プロジェクトでメソッドを呼び出すのとよく似た方法で、データをパラメーターとしてメソッドに渡すことができます。
ネットワークコールバックを使用すると、プレイヤーの参加/離脱、ゲームオブジェクトの作成/破棄、新しいシーンのロードなど、ゲームの進行中に発生するビルトインの Unity イベントを捕らえることができます。 実装できるネットワークコールバックには、以下の 2 種類があります。
NetworkManager コールバック。ネットワークマネージャー自体に関連するコールバック (クライアントの接続/切断など)。
NetworkBehaviour コールバック。ネットワーク化された各ゲームオブジェクトに関連するコールバック (いつ Start 関数の呼び出しを行うか、または、新しプレイヤーがゲームに参加すると、この特定のゲームオブジェクトが何を行うか、など)。
ネットワークメッセージ は、ネットワークの「高レベル API」の一部とされていますが、メッセージを送信するための「低レベル」のアプローチです。ネットワークメッセージでは、スクリプトを使用してクライアントとサーバー間で直接データを送信できます。最も良く使われる Unity の型 (Vector3 など) だけでなく、基本型のデータ (int、文字列など) も送信できます。独自に実装するので、これらのメッセージは特定のゲームオブジェクトや Unity イベントと直接関連していません。自身で目的を決めて実装してください。
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.