Message Events

Inherit from EasyManager.cs

using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}

Inject EasyEvents

using EasyCodeForVivox.Events;
using UnityEngine;
using Zenject;

public class VivoxEvents : MonoBehaviour
{
    EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
}

Dynamic Events

Make sure Dynamic Events are enabled

How to Subscribe to Message Events in EasyCode

EasyManager

Keeping the base methods are not necessary. They are simply Debug.Logs(). Feel free to delete them

// Message Event Callbacks

protected override void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    base.OnChannelMessageRecieved(textMessage);
}

protected override void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    base.OnDirectMessageRecieved(directedTextMessage);
}

protected override void OnDirectMessageFailed(IFailedDirectedTextMessage failedMessage)
{
    base.OnDirectMessageFailed(failedMessage);
}

EasyEvents

public void SubscribeToMessageEvents()
{
    _events.ChannelMessageRecieved += OnChannelMessageRecieved;

    _events.DirectMessageRecieved += OnDirectMessageRecieved;
    _events.DirectMessageFailed += OnDirectMessageFailed;
}

public void UnsubscribeToMessageEvents()
{
    _events.ChannelMessageRecieved -= OnChannelMessageRecieved;

    _events.DirectMessageRecieved -= OnDirectMessageRecieved;
    _events.DirectMessageFailed -= OnDirectMessageFailed;
}



protected virtual void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
}

// This feature is expiramental and may be removed later
protected virtual void OnEventMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
}

protected virtual void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
}

protected virtual void OnDirectMessageFailed(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)]
private void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
}

// This feature is expiremental and may be removed later
[ChannelMessageEvent(ChannelMessageStatus.EventMessageRecieved)]
private void OnEventMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
}

[DirectMessageEvent(DirectMessageStatus.DirectMessageRecieved)]
private void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
}

[DirectMessageEvent(DirectMessageStatus.DirectMessageFailed)]
private void OnDirectMessageFailed(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)]
private async void OnChannelMessageRecievedAsync(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
    await SavePlayerData();
}

// This feature is expiremental and may be removed later
[ChannelMessageEventAsync(ChannelMessageStatus.EventMessageRecieved)]
private async void OnEventMessageRecievedAsync(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
    await SavePlayerData();
}

[DirectMessageEventAsync(DirectMessageStatus.DirectMessageRecieved)]
private async void OnDirectMessageRecievedAsync(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
    await SavePlayerData();
}

[DirectMessageEventAsync(DirectMessageStatus.DirectMessageFailed)]
private async void OnDirectMessageFailedAsync(IFailedDirectedTextMessage failedMessage)
{
    Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");
    await SavePlayerData();
}

Last updated