Sign in to a game

After you initialize the Vivox SDK, you can sign a user in to the Vivox instance assigned to your game.

The name being used for players will either be the Unity AuthenticationId, or a new GUID, if Unity Authentication is not being used.

Note: Do not include your issuer or domain as a part of the username that you pass to the Account constructor. The Account constructor handles this for you.

After initialization, call the VivoxService.Instance.LoginAsync method to sign in to Vivox.

  • The optional argument of VivoxService.Instance.LoginAsync is a LoginOptions struct that can set a display name for the user, enable Text-to-Speech, or load in a blocked user list.

If a display name is set in LoginOptions, this name is visible to all users in channels they join; they receive it as DisplayName within the VivoxParticipant results from VivoxService.Instance.ParticipantAddedToChannel or VivoxService.Instance.ParticipantRemovedFromChannel.

  • The display name is only valid for the current session and does not persist in the Vivox network.
  • The maximum display name length is 127 bytes.

Note: The Vivox SDK does not perform checks on the display name. It is the game developer's responsibility to ensure that the display name follows the rules of the game, that the font characters are supported by the in-game renderer, and that the name is checked for duplication, obscenity, and impersonation. We recommend that you check display names by using some out-of-band means (such as the game server), and not by using the game client.

The following code is an example of how to initiate the sign-in process, setting the DisplayName to "Bob", and enabling Text-to-Speech.

using UnityEngine;
using Unity.Services.Vivox;

class LogInExample : MonoBehaviour
{
    . . .
    async void LoginUserAsync()
    {
        // For this example, the VivoxService is initialized.
        var loginOptions = new LoginOptions()
        {
            DisplayName = "Bob",
            EnableTTS = true
        };
        VivoxService.Instance.LoginAsync(loginOptions)
    }
    . . .
}

You can subscribe to VivoxService.Instance.LoggedIn and VivoxService.Instance.LoggedOut in order to get events when LoginAsync has been successfully called.

The following code is an example of how to subscribe to the VivoxService.Instance.LoggedIn and VivoxService.Instance.LoggedOut events, and how to handle the functions for the different LoginState values:

using UnityEngine;
using Unity.Services.Vivox;

class LoginPropChangeExample : MonoBehaviour
{
    . . .
    VivoxService.Instance.LoggedIn += onLoggedIn;
    VivoxService.Instance.LoggedOut += onLoggedOut;
    . . .

    private void onLoggedIn()
    {
        // Operations as needed
    }

    private void onLoggedOut()
    {
        // Operations as needed
    }
}

Note: We recommend that a user is signed in when an application starts. In scenarios where users can sign in to different accounts within an app, we recommend that they are signed in when they connect to the game server.