publicvoidSubscribeToMessageEvents(){_events.ChannelMessageRecieved+= OnChannelMessageRecieved;_events.DirectMessageRecieved+= OnDirectMessageRecieved;_events.DirectMessageFailed+= OnDirectMessageFailed;}publicvoidUnsubscribeToMessageEvents(){_events.ChannelMessageRecieved-= OnChannelMessageRecieved;_events.DirectMessageRecieved-= OnDirectMessageRecieved;_events.DirectMessageFailed-= OnDirectMessageFailed;}protectedvirtualvoidOnChannelMessageRecieved(IChannelTextMessage textMessage){Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");}// This feature is expiramental and may be removed laterprotectedvirtualvoidOnEventMessageRecieved(IChannelTextMessage textMessage){Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");}protectedvirtualvoidOnDirectMessageRecieved(IDirectedTextMessage directedTextMessage){Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");}protectedvirtualvoidOnDirectMessageFailed(IFailedDirectedTextMessage failedMessage){Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");}
Dynamic Events
Make sure the parameter in your method matches the event type. See what parameter is required for each Message Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=
[ChannelMessageEvent(ChannelMessageStatus.ChannelMessageRecieved)]privatevoidOnChannelMessageRecieved(IChannelTextMessage textMessage){Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");}// This feature is expiremental and may be removed later[ChannelMessageEvent(ChannelMessageStatus.EventMessageRecieved)]privatevoidOnEventMessageRecieved(IChannelTextMessage textMessage){Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");}[DirectMessageEvent(DirectMessageStatus.DirectMessageRecieved)]privatevoidOnDirectMessageRecieved(IDirectedTextMessage directedTextMessage){Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");}[DirectMessageEvent(DirectMessageStatus.DirectMessageFailed)]privatevoidOnDirectMessageFailed(IFailedDirectedTextMessage failedMessage){Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");}
Dynamic Async Events
Make sure the parameter in your method matches the event type. See what parameter is required for each Message Event here. Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this there is no need to Subscribe/Unsubscribe from events with the usual +=/-=
Remember to use async void or async Task or else the event may run synchronously
More information on the methods called in any async method can be found here. Unity Gaming Services Examples. They are direct copies from Unity's docs. These are just examples and don't mimic real world use cases
[ChannelMessageEventAsync(ChannelMessageStatus.ChannelMessageRecieved)]privateasyncvoidOnChannelMessageRecievedAsync(IChannelTextMessage textMessage){Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");awaitSavePlayerData();}// This feature is expiremental and may be removed later[ChannelMessageEventAsync(ChannelMessageStatus.EventMessageRecieved)]privateasyncvoidOnEventMessageRecievedAsync(IChannelTextMessage textMessage){Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");awaitSavePlayerData();}[DirectMessageEventAsync(DirectMessageStatus.DirectMessageRecieved)]privateasyncvoidOnDirectMessageRecievedAsync(IDirectedTextMessage directedTextMessage){Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");awaitSavePlayerData();}[DirectMessageEventAsync(DirectMessageStatus.DirectMessageFailed)]privateasyncvoidOnDirectMessageFailedAsync(IFailedDirectedTextMessage failedMessage){Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");awaitSavePlayerData();}