docs.unity3d.com
    Warning

    Warning: Unity Simulation is deprecated as of December 2023, and is no longer available.

    Implementing an IMessage

    An IMessage is the interface for all messages handled by an IConnector interface. This page captures an example IMessage named DummyConnectionMessage. This message contains two fields: "messageTypeId" and "text"

    public void WriteTo(IMessageSerializer serializer);

    The WriteTo method is an IMessage interface method and must be implemented.

    DummyConnectionMessage writes the two field names ("messageTypeId" and "text") and then those two field values.

    public MessageTypeId MessageTypeId { get; }

    The MessageTypeId property is an IMessage interface property and must be implemented.

    DummyConnectionMessage returns a MessageTypeId: { "MessageTypeName": "DummyConnectionMessage", "Disambiguator": "Default" }

    public static void Register();

    At startup, the IMessage must be registered with the MessageTypeRegistry so the message can be deserialized.

    The [UnityEditor.InitializeOnLoadMethod] attribute will call the Register method (and register the message) when the Unity Editor loads the project for authoring.

    The [UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)] attribute will call the Register method (and register the method) when the Unity Runtime starts the executable for execution.

    public static DummyConnectionMessage Deserialize(IMessageDeserializer deserializer);

    The Deserialize method was registered during the Register() method, above. The Deserialize method is invoked by the MessageTypeRegistry to deserialize the message into an IMessage (in this case, DummyConnectionMessage) instance.

    DummyConnectionMessage reads the two field names ("messageTypeId" and "text") and then those two field values.

    /// <summary>
    /// An IMessage implementation for DummyConnection tests
    /// </summary>
    public class DummyConnectionMessage : IMessage
    {
    
        // The name of the message type: "DummyConnectionMessage"
        static string k_MessageTypeName = typeof(DummyConnectionMessage).Name;
    
        // This message has two fields: "messageTypeId" and "text"
        static string[] s_FieldNames = { "messageTypeId", "text" };
    
        // Deserialize a message into a DummyConnectionMessage
        private DummyConnectionMessage(
            IMessageDeserializer deserializer)
        {
    
            // read the fields
            deserializer.BeginMessage(s_FieldNames);
    
            // read the messageTypeId value
            string messageTypeId;
            deserializer.Read(out messageTypeId);
    
            // read the text value
            string text;
            deserializer.Read(out text);
            this.Text = text;
    
            // done
            deserializer.EndMessage();
        }
    
        public DummyConnectionMessage(
            string text)
        {
            this.Text = text;
        }
    
        // The IMessage MessageTypeId property. This will return "DummyConnectionMessage" (through k_MessageTypeName) and MessageDisambiguator.Default.
        public static readonly MessageTypeId k_MessageTypeId = new MessageTypeId(k_MessageTypeName);
        public static DummyConnectionMessage Deserialize(IMessageDeserializer deserializer) => new DummyConnectionMessage(deserializer);
    
        public MessageTypeId MessageTypeId => k_MessageTypeId;
    
        public string Text { get; private set; }
    
        // Write the message to the serializer
        public void WriteTo(
            IMessageSerializer serializer)
        {
            // write the fields
            serializer.BeginMessage(s_FieldNames);
    
            // write the messageType value
            serializer.Write(this.MessageTypeId.ToString());
    
            // write the text value
            serializer.Write(this.Text);
    
            // done
            serializer.EndMessage();
        }
    
        // Register this type
    #if UNITY_EDITOR
        [UnityEditor.InitializeOnLoadMethod]
    #else
        [UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)]
    #endif
        public static void Register()
        {
            MessageTypeRegistry.Register(k_MessageTypeId, Deserialize);
        }
    }
    

    Updated 2022-08-31T23:11:26.000Z


    In This Article
    • public void WriteTo(IMessageSerializer serializer);
    • public MessageTypeId MessageTypeId { get; }
    • public static void Register();
    • public static DummyConnectionMessage Deserialize(IMessageDeserializer deserializer);
    Copyright © 2023 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX.