Class MessageBase | Multiplayer HLAPI | 1.0.8
docs.unity.cn
    Show / Hide Table of Contents

    Class MessageBase

    Network 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.

    Note : Unity uses its own network serialization system. It doesn't support the NonSerialized attribute. Instead, use private variables.

    In the example below, the methods have implementations, but if those methods were not implemented, the message would still be usable.

    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);
       }
    }
    Inheritance
    Object
    MessageBase
    ConnectLocalClientWorks.TestMessage
    CSHelloMessage
    CSUpdateMessage
    HavingManyLocalClientsSimultaneouslyWorks.TestMessage
    SCUpdateMessage
    AddPlayerMessage
    EmptyMessage
    ErrorMessage
    IntegerMessage
    PeerAuthorityMessage
    PeerInfoMessage
    PeerListMessage
    ReconnectMessage
    RemovePlayerMessage
    StringMessage
    Namespace: UnityEngine.Networking
    Syntax
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public abstract class MessageBase

    Methods

    Deserialize(NetworkReader)

    This method is used to populate a message object from a NetworkReader stream.

    Developers may implement this method for precise control of serialization, but they do no have to. An implemenation of this method will be generated for derived classes.

    Declaration
    public virtual void Deserialize(NetworkReader reader)
    Parameters
    Type Name Description
    NetworkReader reader

    Stream to read from.

    Serialize(NetworkWriter)

    The method is used to populate a NetworkWriter stream from a message object.

    Developers may implement this method for precise control of serialization, but they do no have to. An implemenation of this method will be generated for derived classes.

    Declaration
    public virtual void Serialize(NetworkWriter writer)
    Parameters
    Type Name Description
    NetworkWriter writer

    Stream to write to.

    In This Article
    • Methods
      • Deserialize(NetworkReader)
      • Serialize(NetworkWriter)
    Back to top Copyright © 2020 Unity Technologies
    Generated by DocFX
    on 15 December 2020