??
Sun Guangdong 2015.7.12
In addition to the commands and RPC calls of the high level facilities, it is possible to send the original network messages.
There is also a class of network message classes called Messagebase that can be extended to make serializable. This class has serialization and deserialization capabilities for read/write objects. Developers can execute these functions themselves, or rely on code generation implementations that are automatically created through network systems. The base class looks like this:
Public abstract class messagebase{ //de-serialize The contents of the reader to this message public virtual Voi D Deserialize (Networkreader reader) {} //Serialize The contents of this message to the writer public virtual VO ID Serialize (networkwriter writer) {}}
There is a built-in message class for commonly used network messages types:
Emptymessage
Stringmessage
Integermessage
Sending a message has the Send () function, which works the same way in Networkclient, Networkserver, and Networkconnection. They derive the message Id and the message 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 Networkclie NT (); 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 does not have serialization code.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Unity5.1 new network engine unet (v) unet network Messages