How to Login with VivoxVoiceManager

This tutorial will show you how to get started using VivoxVoiceManager.cs from Unity's Channel Sample App. Since Unity is always updating their Vivox Package from the Package Manager I will not do a deep dive but just show you the very basics to get up and running with Vivox.

This blog will be updated over time and may be out of date or incomplete when you come across it. Let me know in the comments and I will try and update when I get time.

Want to do more with Vivox

Also, if you don't want to implement Vivox from scratch or want to use more of Vivox features than are provided in Unity's sample app make sure to check out my free asset EasyCodeForVivox in the Unity Asset Store (EasyCodeForVivox Does not support Unity Authentication at the moment. You have to input your credentials manually)

Need More Help

If you need help with any Vivox related questions, check out the Unity Vivox forums or join the Full Stack Indie Discord and someone from the community will try and help you

Getting Started

Let's get started using Vivox with the VivoxVoiceManager.cs

I will be creating a class called VivoxManager.cs and will start with getting the Singleton instance of the VivoxVoiceManager. There must be 1 VivoxVoiceManager on a game object in the scene.

If you linked your project to Unity Gaming Services (UGS) then your credentials will automatically be loaded, and you can leave the 4 fields below empty. If you didn't use UGS, Unity Authentication, or created a project using the Vivox Developer Portal then you must manually input your credentials on the VivoxVoiceManager game object.

You may get an error saying you still need add Unity's Authentication service if you didn't link your project in Project Settings but VivoxVoiceManager will still work if you input your credentials manually

VivoxVoiceManagerCredentials.png

If you don't know the '_' prefix is used for private member variables

private VivoxVoiceManager _vivoxManager;

    private void Awake()
    {
        _vivoxManager = VivoxVoiceManager.Instance;
    }

Login To Vivox

To Login to Vivox create a public method that can be attached to Buttons in Unity's UI.

public void Login()
    {
        _vivoxManager.Login();
        _vivoxManager.Login("murph");
    }
  • _vivoxManager.Login() - Logs in a player and gives them the Id from Unity's Authentication Service or creates a random Id
  • _vivoxManager.Login("murph") - Logs in a player with the Display Name "murph" and gives them the Id from Unity's Authentication Service or creates a random Id. Recommended to use Display Name for your players

To access the Player Id (Name) or Display Name you can use the current LoginSession

    private void DisplayPlayerIds()
    {
        // Unity's Authentication service - if you are using
        Debug.Log($"{AuthenticationService.Instance.PlayerId}");

        // Login Session
        Debug.Log($"{_vivoxManager.LoginSession.LoginSessionId.Name}");
        Debug.Log($"{_vivoxManager.LoginSession.LoginSessionId.DisplayName}");
    }
  • AuthenticationService.Instance.PlayerId - Is used to get the player Id (Unique Identifier) of the user.

  • _vivoxManager.LoginSession.LoginSessionId .Name - Is used to get the player Id (Unique Identifier) of the user.

  • _vivoxManager.LoginSession.LoginSessionId.DisplayName - Is used to get the Display Name of the player and is what will be/should be used when displaying username to other players

Subscribe to Login Events

If you want to be alerted when a Player signs in or out, then you want to create 2 methods private void SubscribeToLoginEvents() and private void UnsubscribeToLoginEvents()

Inside of these methods we will Subscribe/Unsubscribe to the Login Events available to us from the VivoxVoiceManager.

private void SubscribeToLoginEvents()
{
    _vivoxManager.OnUserLoggedInEvent += OnPlayerLoggedIn;
    _vivoxManager.OnUserLoggedInEvent += OnPlayerLoggedIn;
}

private void UnsubscribeToLoginEvents()
{
    _vivoxManager.OnUserLoggedInEvent -= OnPlayerLoggedIn;
    _vivoxManager.OnUserLoggedInEvent -= OnPlayerLoggedIn;
}

Next, we want to create the methods that be invoked/fired when these Vivox events happen. All these methods do is write a console message letting us know the username/player's name of the user who has successfully logged in/out

private void OnPlayerLoggedIn()
    {
        Debug.Log($"Player {_vivoxManager.LoginSession.LoginSessionId.DisplayName} has Successfully Logged In");
    }

    private void OnPlayerLoggedOut()
    {
        Debug.Log($"Player {_vivoxManager.LoginSession.LoginSessionId.DisplayName} has been Logged Out");
    }

Then we want to Add these methods to the Start() and OnApplicationQuit() methods. We subscribe to be alerted of Login Events and Unsubscribe when the app/game closes to prevent memory leaks.

    private void Start()
    {
        SubscribeToLoginEvents();
    }

    private void OnApplicationQuit()
    {
        UnsubscribeToLoginEvents();
    }

Check if a User Logged In

If you ever need to check if a user is logged in you can do so by checking the LoginState like the code below

private void CheckIfPlayerIsLoggedIn()
{
    if(_vivoxManager.LoginState == VivoxUnity.LoginState.LoggedIn)
    {
        // your code here
    }
}