Easy Code For Vivox
  • Introduction
    • Official Links
    • Getting Started
      • Wheres The Code
      • Setup EasyCode in My Project
    • Setup Demo Scenes
      • Vivox Developer Portal
      • Unity Gaming Services (UGS) Dashboard
    • Things To Consider
    • FAQ
  • Development Info
    • Design Decisions
    • Release Notes
      • v2.0
      • v1.3
      • v1.2
      • v1.1
      • Template
    • Not Supported
  • Easy Code For Vivox
    • How do I setup EasyCode?
      • Setup Your Credentials
    • How do I do this in EasyCode?
      • Login / Logout
        • Login Properties
        • SetTransmissionMode
        • Login Events
      • Join / Leave Channel
        • Audio Channel
          • Audio Channel Events
        • Text Channel
          • Text Channel Events
        • Channel Events
      • Send Messages
        • Message Events
      • Mute / Unmute
        • Mute / Unmute Events
      • Subscribe to User Events
      • Volume / Audio Settings
        • Audio Device Events
      • Text To Speech ( TTS )
        • TTS Events
    • Vivox Access Tokens
      • Unity Cloud Code
    • Supported Vivox Events
      • Callback Methods
    • Folder Structure / Info
      • / Demo Scenes /
      • / Documentation /
      • / Examples /
      • / Plugins /
      • / Resources /
      • / Scripts /
      • / Settings /
    • Common Errors
  • API Info
    • EasySession.cs
    • EasyManager.cs
      • Main Methods
      • Vivox Event Callbacks
    • Easy3DPositional.cs
    • EasyVivoxUtilities.cs
    • EasySettings.cs
    • Extension Methods
      • EasySIPExtensions.cs
      • GameObjectExtensions.cs
      • TTSMessageExtensions.cs
      • UIExtensions.cs
      • VivoxExtensions.cs
      • EasyDebug.cs
    • EasyEvents.cs
  • Dependency Injection
    • Zenject vs Extenject
    • Install Dependencies
    • Inject Classes
  • Dynamic Events
    • Dynamic Events
    • Tests
    • Gotchas
    • Dynamic Async Events
      • Dont Do
    • Event Examples
  • Related Info
    • How do I do this in Vivox?
      • Conference Chat
    • Pre-Processor Directives
    • How to set iOS Info.plist for Unity?
    • Unity Gaming Services
  • The Future
    • Roadmap
    • Todo / Notes / Changelog
Powered by GitBook
On this page
  • Events - Action/Delegates
  • Synchronous - Normal way of doing events
  • Synchronous - Easy Code way of doing events
  • Easy Code Dynamic Events
  • Synchronous - Dynamic Events
  • Asynchronous - Dynamic Async Events

Was this helpful?

  1. Dynamic Events

Event Examples

Events - Action/Delegates

Synchronous - Normal way of doing events

using System;
using UnityEngine;
using VivoxUnity;

public class NormalEvents : MonoBehaviour
{
    public event Action<ILoginSession> LoggingIn;

    void Start()
    {
        LoggingIn += PlayerLoggingIn;
    }
    private void OnApplicationQuit()
    {
        LoggingIn -= PlayerLoggingIn;
    }

    public void PlayerLoggingIn(ILoginSession loginSession)
    {
        Debug.Log($"Invoking Normal Event from {nameof(PlayerLoggingIn)}");
    }
}

Synchronous - Easy Code way of doing events

internal class EasyCodeEvents : MonoBehaviour
{
    private EasyEvents _events;

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

    public void AddEvent()
    {
        _events.LoggedIn += OnLoggedIn;
    }

    public void RemoveEvent()
    {
        _events.LoggedIn -= OnLoggedIn;
    }

    private void OnLoggedIn(ILoginSession loginSession)
    {
        Debug.Log($"User {loginSession.LoginSessionId.DisplayName} has logged in");
    }
}

Easy Code Dynamic Events

Don't want to Subscribe and Unsubscribe from events or use Zenject dependency injection. Use Dynamic events instead. They are a tad bit slower in some cases but events are fire and forget and aren't called in Unity's Update() loop, so you really can't tell the difference until you have hundreds of game objects firing events.

Vivox events arent fired that often so unless you are updating hundreds of gameobejcts based on a players's name or login status you should be fine to use Dynamic Events.

Remember to use async void or async Task when using Dynamic Async events

Synchronous - Dynamic Events

public class DynamicAsyncEvents : MonoBehaviour
{
    [LoginEvent(LoginStatus.LoggingIn)]
    public void LoginCallback(ILoginSession loginSession)
    {
        Debug.Log($"Invoking Async Event Dynamically from {nameof(LoginCallback)}");
    }

    [LoginEvent(LoginStatus.LoggedIn)]
    public void OnLoggedIn(ILoginSession loginSession)
    {
        await Task.Run(() =>
        {
            for (int i = 0; i < 100; i++)
            {
                Debug.Log($"Method Event has been invoked dynamically");
            }
        });
    }
    
}

Asynchronous - Dynamic Async Events

Reminder not to use Dynamic Async Events that modify the UI or GameObjects in Scene .

public class DynamicAsyncEvents : MonoBehaviour
{
    [LoginEventAsync(LoginStatus.LoggedIn)]
    public async void DynamicEventAsyncVoid(ILoginSession loginSession)
    {
        var bytes = Encoding.Unicode.GetBytes(loginSession.LoginSessionId.DisplayName);
        using (FileStream fileStream = new FileStream($"{Directory.GetCurrentDirectory()}\\Assets\\playerName.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, bufferSize: 4096, useAsync: true))
        {
            await fileStream.WriteAsync(bytes, 0, bytes.Length);
        }
        Debug.Log("Done creating text file");
    }

    [LoginEventAsync(LoginStatus.LoggedIn)]
    public async Task AsyncMethod(ILoginSession loginSession)
    {
        await Task.Run(() =>
        {
            for (int i = 0; i < 100; i++)
            {
                Debug.Log($"Async Method Event has been invoked");
            }
        });
    }
}

PreviousDont DoNextHow do I do this in Vivox?

Last updated 2 years ago

Was this helpful?

Read more about [Inject] attribute and

Method Injection here