Select your preferred scripting language. All code snippets will be displayed in this language.
class in UnityEngine.Networking
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseNetwork message classes should be derived from this class. These message classes can then be sent using the various Send functions of NetworkConnection, NetworkClient and NetworkServer.
Public data fields of classes derived from MessageBase will be automatically serialized with the class. The virtual methods Serialize and Deserialize may be implemented by developers for precise control, but if they are not implemented, then implementations will be generated for them.
In the example below, the methods have implementations, but if those methods were not implemented, the message would still be usable.
#pragma strict class SpawnMessage extends MessageBase { public var netId; public var assetId; public var position; public var payload; // This method would be generated public override function Deserialize(reader) { netId = reader.ReadPackedUInt32(); assetId = reader.ReadNetworkHash128(); position = reader.ReadVector3(); payload = reader.ReadBytesAndSize(); } // This method would be generated public override function Serialize(writer) { writer.WritePackedUInt32(netId); writer.Write(assetId); writer.Write(position); writer.WriteBytesFull(payload); } }
using UnityEngine; using UnityEngine.Networking;
class SpawnMessage : MessageBase { public uint netId; public NetworkHash128 assetId; public Vector3 position; public byte[] payload;
// This method would be generated public override void Deserialize(NetworkReader reader) { netId = reader.ReadPackedUInt32(); assetId = reader.ReadNetworkHash128(); position = reader.ReadVector3(); payload = reader.ReadBytesAndSize(); }
// This method would be generated public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); writer.Write(assetId); writer.Write(position); writer.WriteBytesFull(payload); } }
Deserialize | This method is used to populate a message object from a NetworkReader stream. |
Serialize | The method is used to populate a NetworkWriter stream from a message object. |
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