重要: UNet は非推奨のソリューションであり、新しい Multiplayer and Networking Solution (MLAPI) が開発途中です。詳細については、Unity MLAPI ウェブサイトの情報 を参照してください。 |
「高レベル」コマンドと遠隔手続き呼び出し (RPC) に加えて、生のネットワークメッセージも送信することが可能です。
MessageBase と呼ばれるクラスは、シリアライズ可能なネットワークメッセージクラスを作成するために拡張することができます。このクラスは、パラメーターとして reader と writer のオブジェクトを取る Serialize と Deserialize 関数を持っています。開発者はこれらの関数を自分で実装することもできますが、ネットワークシステムによって自動的に作成される、コード生成による実装に任せることもできます。基本クラスは以下の通りです。
public abstract class MessageBase
{
// reader のコンテンツをメッセージにデシリアライズします
public virtual void Deserialize(NetworkReader reader) {}
// メッセージのコンテンツを writer にシリアライズします
public virtual void Serialize(NetworkWriter writer) {}
}
Message のクラスには、基本型、構造体、配列、Unity のほとんどの通常の型 (Vector3 など) のメンバーが含まれます。複雑なクラスやジェネリックのコンテナのメンバーは含まれません。コード生成による実装に任せたい場合は、型がパブリックに見えるようにしておく必要があります。
一般的なタイプのネットワークメッセージのためにビルトインのメッセージクラスがあります。
メッセージを送信するには、NetworkClient、NetworkServer、NetworkConnection クラスのSend()
関数を使用します。これらの関数はすべて同様に機能します。また、Send 関数は、MessageBase から派生したメッセージ ID とメッセージオブジェクトをパラメーターとして取ります。以下のコードでは、ビルトインのメッセージクラスの 1 つを使用したメッセージの送信と処理方法を示しています。
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class Begin : NetworkBehaviour
{
const short MyBeginMsg = 1002;
NetworkClient m_client;
public void SendReadyToBeginMessage(int myId)
{
var msg = new IntegerMessage(myId);
m_client.Send(MyBeginMsg, msg);
}
public void Init(NetworkClient client)
{
m_client = client;
NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);
}
void OnServerReadyToBeginMessage(NetworkMessage netMsg)
{
var beginMessage = netMsg.ReadMessage<IntegerMessage>();
Debug.Log("received OnServerReadyToBeginMessage " + beginMessage.value);
}
}
カスタムのネットワークメッセージのクラスを宣言し、それを使用する例は以下の通りです。
using UnityEngine;
using UnityEngine.Networking;
public class Scores : MonoBehaviour
{
NetworkClient myClient;
public class MyMsgType {
public static short Score = MsgType.Highest + 1;
};
public class ScoreMessage : MessageBase
{
public int score;
public Vector3 scorePos;
public int lives;
}
public void SendScore(int score, Vector3 scorePos, int lives)
{
ScoreMessage msg = new ScoreMessage();
msg.score = score;
msg.scorePos = scorePos;
msg.lives = lives;
NetworkServer.SendToAll(MyMsgType.Score, msg);
}
// クライアントを作成し、サーバーポートに接続します
public void SetupClient()
{
myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.RegisterHandler(MyMsgType.Score, OnScore);
myClient.Connect("127.0.0.1", 4444);
}
public void OnScore(NetworkMessage netMsg)
{
ScoreMessage msg = netMsg.ReadMessage<ScoreMessage>();
Debug.Log("OnScoreMessage " + msg.score);
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}
この例の中には ScoreMessage
クラスのシリアライゼーションコードはありません。このクラスのシリアライゼーション関数の本体は Unity によって自動的に作成されます。
ErrorMessage クラスもあり、これは MessageBase
から派生します。このクラスは、クライアントおよびサーバーのエラーコールバックへ渡されます。
ErrorMessage クラスの errorCode は Networking.NetworkError 列挙型に対応します。
class MyClient
{
NetworkClient client;
void Start()
{
client = new NetworkClient();
client.RegisterHandler(MsgType.Error, OnError);
}
void OnError(NetworkMessage netMsg)
{
var errorMsg = netMsg.ReadMessage();
Debug.Log("Error:" + errorMsg.errorCode);
}
}
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.