Unity5.1 new Network engine UNET (5) UNET Network Messages, unity5.1unet
Sun Guangdong
In addition to the high level facilities command and RPC call, it is also possible to send the original network message.
Another type is called MessageBase, which can be expanded to enable serialization of network messages. This class provides the serialization and deserialization functions for read/write objects. Developers can execute these functions themselves, or rely on the code generated automatically by the network system. The base class looks like this:
public abstract class MessageBase{ // De-serialize the contents of the reader into this message public virtual void Deserialize(NetworkReader reader) {} // Serialize the contents of this message into the writer public virtual void Serialize(NetworkWriter writer) {}}
Built-in message classes are commonly used network messages:
EmptyMessage
StringMessage
IntegerMessage
A message can be sent using the Send () function, which works in the same way as NetworkClient, NetworkServer, and NetworkConnection. They derive the Message Id and object from MessageBase. The following code shows how to send and process messages, using one of the built-in message classes:
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); }}
Declare a custom network message class and use it:
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); } // Create a client and connect to the server port 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"); }}
Note that the ScoreMessage class in this example has no serialization code.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.