# Text Channel Events

## Inherit from EasyManager.cs

```csharp
using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}
```

## Inject EasyEvents

```csharp
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

<figure><img src="https://3152232848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWHF2U_tfZEUaf_IsL-%2Fuploads%2FgNWd8mgjCB90w9n3fR8A%2FEnableDynamicEvents.png?alt=media&#x26;token=f9e1f6a3-8d21-49a1-9ff5-d1a8151efad1" alt=""><figcaption></figcaption></figure>

## How to Subscribe to Text Channel Events in EasyCode&#x20;

#### EasyManager

{% hint style="info" %}
Keeping the *<mark style="color:blue;">**base**</mark>* methods are not necessary. They are simply <mark style="color:blue;">**Debug.Logs()**</mark>. Feel free to delete them
{% endhint %}

```csharp
// Text Channels Event Callbacks

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

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

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

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

#### EasyEvents

```csharp
public void SubscribeToTextChannelEvents()
{
    _events.TextChannelConnecting += OnTextChannelConnecting;
    _events.TextChannelConnected += OnTextChannelConnected;
    _events.TextChannelDisconnecting += OnTextChannelDisconnecting;
    _events.TextChannelDisconnected += OnTextChannelDisconnected;
}

public void UnsubscribeToTextChannelEvents()
{
    _events.TextChannelConnecting -= OnTextChannelConnecting;
    _events.TextChannelConnected -= OnTextChannelConnected;
    _events.TextChannelDisconnecting -= OnTextChannelDisconnecting;
    _events.TextChannelDisconnected -= OnTextChannelDisconnected;
}


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

protected virtual void OnTextChannelConnected(IChannelSession channelSession)
{
        Debug.Log($"{channelSession.Channel.Name} Text Has Connected In Channel");
}

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

protected virtual void OnTextChannelDisconnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Text Has Disconnected In Channel");
}
```

#### Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each [AudioChannel Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#audio-channel-events). 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 <mark style="color:red;">**+=/-=**</mark>

```csharp
[TextChannelEvent(TextChannelStatus.TextChannelConnecting)]
private void OnTextChannelConnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
}

[TextChannelEvent(TextChannelStatus.TextChannelConnected)]
private void OnTextChannelConnected(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
}
[TextChannelEvent(TextChannelStatus.TextChannelDisconnecting)]
private void OnTextChannelDisconnecting(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
}

[TextChannelEvent(TextChannelStatus.TextChannelDisconnected)]
private void OnTextChannelDisconnected(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 [AudioChannel Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#audio-channel-events). 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 <mark style="color:red;">**+=/-=**</mark>

{% hint style="info" %}
**Remember to use&#x20;**<mark style="color:blue;">**async void**</mark>**&#x20;or&#x20;**<mark style="color:blue;">**async Task**</mark>**&#x20;or else the event may run synchronously**
{% endhint %}

{% hint style="warning" %}
**More information on the methods called in any async method can be found here.** [**Unity Gaming Services Examples**](https://fullstackindie.gitbook.io/easy-code-for-vivox/related-info/unity-gaming-services)**. They are direct copies from Unity's docs.&#x20;**<mark style="color:red;">**These are just examples and don't mimic real world use cases**</mark>
{% endhint %}

<pre class="language-csharp"><code class="lang-csharp"><strong>[TextChannelEventAsync(TextChannelStatus.TextChannelConnecting)]
</strong>private async void OnTextChannelConnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Connecting");
    await LoadPlayerData();
}

[TextChannelEventAsync(TextChannelStatus.TextChannelConnected)]
private async void OnTextChannelConnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Connected : Channel Type == {channelSession.Channel.Type}");
    await GetJoinedLobbies();
}
[TextChannelEventAsync(TextChannelStatus.TextChannelDisconnecting)]
private async void OnTextChannelDisconnectingAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Is Disconnecting");
    await SavePlayerData();
}

[TextChannelEventAsync(TextChannelStatus.TextChannelDisconnected)]
private async void OnTextChannelDisconnectedAsync(IChannelSession channelSession)
{
    Debug.Log($"{channelSession.Channel.Name} Has Disconnected");
    await RemovePlayerFromLobby();
}
</code></pre>
