Channel 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 Channel Events in EasyCode

EasyManager

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

// Channel Event Callbacks

protected override void OnChannelConnecting(IChannelSession channelSession)
{
    base.OnChannelConnecting(channelSession);
}

protected override void OnChannelConnected(IChannelSession channelSession)
{
    base.OnChannelConnected(channelSession);
}

protected override void OnChannelDisconnecting(IChannelSession channelSession)
{
    base.OnChannelDisconnecting(channelSession);
}

protected override void OnChannelDisconnected(IChannelSession channelSession)
{
    base.OnChannelDisconnected(channelSession);
}

EasyEvents

public void SubscribeToChannelEvents()
{
    _events.ChannelConnecting += OnChannelConnecting;
    _events.ChannelConnected += OnChannelConnected;
    _events.ChannelDisconnecting += OnChannelDisconnecting;
    _events.ChannelDisconnected += OnChannelDisconnected;
}

public void UnsubscribeToChannelEvents()
{
    _events.ChannelConnecting -= OnChannelConnecting;
    _events.ChannelConnected -= OnChannelConnected;
    _events.ChannelDisconnecting -= OnChannelDisconnecting;
    _events.ChannelDisconnected -= OnChannelDisconnected;
}

protected virtual void OnChannelConnecting(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Is Connecting");
}

protected virtual void OnChannelConnected(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
}

protected virtual void OnChannelDisconnecting(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
}

protected virtual void OnChannelDisconnected(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
}

Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each Channel 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 +=/-=

[ChannelEvent(ChannelStatus.ChannelConnecting)]
private void OnChannelConnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
}

[ChannelEvent(ChannelStatus.ChannelConnected)]
private void OnChannelConnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
}

[ChannelEvent(ChannelStatus.ChannelDisconnecting)]
private void OnChannelDisconnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
}

[ChannelEvent(ChannelStatus.ChannelDisconnected)]
private void OnChannelDisconnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
}

Dynamic Async Events

Make sure the parameter in your method matches the event type. See what parameter is required for each Channel 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

[ChannelEventAsync(ChannelStatus.ChannelConnecting)]
private async void OnChannelConnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
    await LoadPlayerData();
}

[ChannelEventAsync(ChannelStatus.ChannelConnected)]
private async void OnChannelConnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
    await GetJoinedLobbies();
}

[ChannelEventAsync(ChannelStatus.ChannelDisconnecting)]
private async void OnChannelDisconnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
    await SavePlayerData();
}

[ChannelEventAsync(ChannelStatus.ChannelDisconnected)]
private async void OnChannelDisconnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
    await RemovePlayerFromLobby();
}

Last updated