# 2. Initialize, Login and Service Lifecycle
Part of [[Voice Chat with Vivox]]. Back: [[1. Installing Vivox and UGS Credentials]]. Next: [[3. Group Channels and Participants]].
> [!warning] Code not compiled in Unity
> Listings follow the official Vivox quickstart and the 16.12.1 Scripting API (`IVivoxService.InitializeAsync`, `LoginAsync`, `LogoutAsync`, `Uninitialize`, `InitializationState`). Not compiled against `Unity.Services.Vivox.dll`.
## 2.1 Official order
The official quickstart order, when Unity Authentication is in the project:
1. `await UnityServices.InitializeAsync();`
2. `await AuthenticationService.Instance.SignInAnonymouslyAsync();` — same call as [[1. Services Project and Authentication]]. Sign in **once**; do not sign in a second time just for Vivox.
3. `await VivoxService.Instance.InitializeAsync();` (`InitializeAsync(VivoxConfigurationOptions options = null)` in the 16.12.1 API).
4. `await VivoxService.Instance.LoginAsync(options);`
Until step 3 has finished, do not join a channel. `IVivoxService.InitializationState` is the official state enum (`Uninitialized`, `Initializing`, `Initialized`, `Failed`). Events: `Initialized`, `InitializationFailed`.
`VivoxService.Instance` is a **Service Locator**. Wrap it so classroom UI and tests do not scatter `Instance` calls; do not add a second singleton on top of it.
## 2.2 LoginOptions
Official 16.12.1 `LoginOptions`:
| Property | Use |
|---|---|
| `DisplayName` | What other participants see. Official remarks: **not** intended to be the unique ID of a user. Put the student's chosen name here, not `ISession` internals. |
| `EnableTTS` | Text-to-speech for this account. Leave `false` unless you are actually shipping TTS. |
| `PlayerId` | Official remarks: an override **only** when the Authentication SDK is **not** in use. With Authentication, the local user is signed into Vivox with the Authentication `PlayerId`. Do not set this in a Lobby-backed project. |
| `BlockedUserList` | Account identifiers to block immediately on login. |
| `ParticipantUpdateFrequency` | How often participant update events fire. Leave default unless you are building a custom VU meter and have measured the cost. |
| `SpeechToTextLanguages` | Optional BCP-47 hints; default is official "en". |
```csharp
using System;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Vivox;
using UnityEngine;
/// <summary>
/// Official Vivox 16.12.1 lifecycle on top of the same UGS + Authentication
/// path as the Lobby series. Wrap VivoxService.Instance; do not scatter it.
/// </summary>
public sealed class VivoxLifecycle : MonoBehaviour
{
[SerializeField] string displayName = "Student";
public bool IsLoggedIn =>
VivoxService.Instance != null && VivoxService.Instance.IsLoggedIn;
public string SignedInPlayerId =>
VivoxService.Instance != null ? VivoxService.Instance.SignedInPlayerId : string.Empty;
public async Task InitializeAndLoginAsync(string playerDisplayName)
{
if (UnityServices.State != ServicesInitializationState.Initialized)
await UnityServices.InitializeAsync();
if (!AuthenticationService.Instance.IsSignedIn)
await AuthenticationService.Instance.SignInAnonymouslyAsync();
var vivox = VivoxService.Instance;
if (vivox.InitializationState != VivoxInitializationState.Initialized)
await vivox.InitializeAsync();
if (vivox.IsLoggedIn)
return;
var name = string.IsNullOrWhiteSpace(playerDisplayName) ? displayName : playerDisplayName;
await vivox.LoginAsync(new LoginOptions
{
DisplayName = name,
EnableTTS = false
});
}
public async Task LogoutAsync()
{
var vivox = VivoxService.Instance;
if (vivox == null)
return;
if (vivox.IsLoggedIn)
await vivox.LogoutAsync();
}
/// <summary>
/// Fully tears the SDK down so you can InitializeAsync again later.
/// Official 16.12.0 addition on IVivoxService. Call LogoutAsync first.
/// Typical use is quitting the app, or a rare "switch accounts" flow.
/// </summary>
public async Task LogoutAndUninitializeAsync()
{
await LogoutAsync();
var vivox = VivoxService.Instance;
if (vivox != null && vivox.InitializationState == VivoxInitializationState.Initialized)
vivox.Uninitialize();
}
}
```
`ServicesInitializationState` and `AuthenticationService.Instance.IsSignedIn` are the same guards the Lobby notes already use. If those types differ in a later Authentication patch, follow the Lobby series, not this snippet.
`SignedInPlayerId` is empty until login succeeds. With Authentication in the project, this is the UAS player id — the same id `VivoxParticipant.PlayerId` will report for this user.
## 2.3 Logout and Uninitialize
Official quickstart: `LogoutAsync()` signs the user out; after that there is no network traffic to or from the Vivox SDK. Call it when quitting, or when the user can switch accounts.
`LeaveAllChannelsAsync()` first if you are mid-channel and you want a deliberate leave (part 3). Official 16.12.0 changelog fixed channels not being fully cleaned up on logout (stale event subscriptions, duplicated events after re-login). 16.12.1 includes that fix — still leave explicitly from your session hook so your UI state matches.
`Uninitialize()` (void, 16.12.1 API) fully uninitializes the SDK so you can initialize again later. Do not call it between every classroom session; login/logout is enough for "leave the day, come back."
Subscribe to `LoggedIn` / `LoggedOut` if the HUD needs a signal. Unsubscribe on destroy.
Next: [[3. Group Channels and Participants]].