(このクラスは、旧ネットワークシステムの一部であり、非推奨となりました。新システムの NetworkIdentity を参照してください。)
Network View は、Unity でマルチプレイヤーのネットワークゲームを作成するための入り口です。使いやすく、強力です。このため、ネットワークビューで作業を開始する前に、ネットワークに関する基本的な原理を理解しておくことをお勧めします。旧ネットワークリファレンスガイド で基本的な原理を学ぶことができます。
State Synchronization や Remote Procedure Calls を含むネットワーク機能を使用するには、ゲームオブジェクト にネットワークビューを追加させる必要があります。
プロパティー | 機能 |
---|---|
State Synchronization | このネットワークビューで使用される 状態同期の詳細 のタイプ。 |
Off | State Synchronization が使用されません。RPC の詳細 のみを送信したい場合に最適です。 |
Reliable Delta Compressed | 最後の状態と現在の状態の差が送信されます。何も変更されていない場合は、何も送信されません。このモードは順序付けられます。パケットが失われた場合は、失われたパケットが自動的に再送信されます。 |
Unreliable | 完全な状態が送信されます。より多くの帯域幅を使用しますが、パケット損失の影響は最小化されます。 |
Observed | ネットワークに送信される コンポーネント データ。 |
View ID | このネットワークビューに対する一意の識別子。これらの値はインスペクターでは読み取り専用です。 |
Scene ID | この特定のシーンでのネットワークビューの ID 番号。 |
Type | ランタイムに Scene か Allocated に保存されます。 |
ゲームオブジェクトをネットワークビューに追加する場合、次の 2 つを決定する必要があります。
ネットワークビューの Observed プロパティーは、1 つのコンポーネントを含むことができます。このコンポーネントには、トランスフォーム 、アニメーション 、リジッドボディ やスクリプトがあります。Observed コンポーネントがなんであれ、それに関するデータがネットワーク上で送信されます。ドロップダウンからコンポーネントを選択するか、コンポーネントヘッダーを直接変数にドラッグできます。RPC コールなどを使用して、直接データを送信しない場合は、同期をオフにし (データが直接送信されません)、Observed プロパティーとして何も設定する必要はありません。RPC コールは、ネットワークビューが 1 つあればよいので、ビューがすでに存在している場合は、RPC にビューを追加する必要はありません。
Observed コンポーネントのデータを送信するには、State Synchronization か Remote Procedure Calls のいずれかを用います。
State Synchronization を使用するには、ネットワークビューの State Synchronization を Reliable Delta Compressed か Unreliable のいずれかに設定します。Observed コンポーネントのデータがネットワーク上で自動的に送信されます。
Reliable Delta Compressed は順序付けられます。パケットは常に送信順に受信されます。パケットが失われると、そのパケットは再送信されます。それ以降のパケットはすべて失われたパケットが受信されるまで、キューに入れられます。最後の送信値と現在の送信値間の差のみが送信され、差がない場合は何も送信されません。
スクリプトに従う場合は、スクリプト内でデータを明確にシリアライズする必要があります。これは OnSerializeNetworkView() 関数内で行います。
using UnityEngine;
using UnityEngine.Network;
using System.Collections;
public class ExampleScript : MonoBehaviour {
void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info) {
float horizontalInput = Input.GetAxis ("Horizontal");
stream.Serialize (horizontalInput);
}
}
C# スクリプトの例
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
var horizontalInput : float = Input.GetAxis ("Horizontal");
stream.Serialize (horizontalInput);
}
JS スクリプトの例
アップデートを受信し、そうでない場合に、ストリームに書きこむ変数からの読み取り値を受信すると、上記の関数は常に (ストリームからのアップデートを) horizontalInput に書き込みます。アップデートを受信や、送信した際にこれと別のことを行いたい場合、BitStream クラスの isWriting 属性を使用できます。
using UnityEngine;
using UnityEngine.Network;
using System.Collections;
public class ExampleScript : MonoBehaviour {
void OnSerializeNetworkView (BitStream stream, NetworkMessageInfo info) {
float horizontalInput = 0.0;
if (stream.isWriting) {
// 送信
horizontalInput = Input.GetAxis ("Horizontal");
stream.Serialize (horizontalInput);
} else {
// 受信
stream.Serialize (horizontalInput);
// ... 受信した変数で何かを行います
}
}
}
C# スクリプトの例
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
var horizontalInput : float = 0.0;
if (stream.isWriting) {
//送信
horizontalInput = Input.GetAxis ("Horizontal");
stream.Serialize (horizontalInput);
} else {
// 受信
stream.Serialize (horizontalInput);
// ... 受信した変数で何かを行います
}
}
JS スクリプトの例
OnSerializeNetworkView が、ネットワークマネージャのプロジェクト設定で指定された sendRate にしたがって呼び出されます。デフォルトでは、これは 1 秒あたり 15 回になります。
スクリプトでリモートプロシージャコールを使用したい場合は、スクリプトが追加された同じゲームオブジェクトにある NetworkView コンポーネントだけ必要になります。NetworkView は、他の何かを行うのに使用でき、あるいは、RPC の送信にのみ使用する場合は、スクリプトに従わず、状態同期化をオフできます。ネットワークから呼び出せる関数には、@RPC 属性が必要です。同じゲームオブジェクトに追加されたスクリプトから、networkView.RPC() を呼び出して、リモートプロシージャコールを実行します。
using UnityEngine;
using UnityEngine.Network;
using System.Collections;
public class ExampleScript : MonoBehaviour {
GameObject playerBullet;
NetworkView networkView;
void Start () {
networkView = new NetworkView ();
}
void Update () {
if (Input.GetButtonDown ("Fire1")) {
networkView.RPC ("PlayerFire", RPCMode.All);
}
}
[RPC]
void PlayerFire () {
Instantiate (playerBullet, playerBullet.transform.position, playerBullet.transform.rotation);
}
}
C# スクリプトの例
var playerBullet : GameObject;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
networkView.RPC ("PlayerFire", RPCMode.All);
}
}
@RPC
function PlayerFire () {
Instantiate (playerBullet, playerBullet.transform.position, playerBullet.transform.rotation);
}
JS スクリプトの例
RPC は高い信頼性で送信され、順序付けられます。RPC の詳細については、RPC の詳細(旧) ページを参照してください。
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.