Vivox Unity SDK 16.0.0 upgrade guide
Upgrade from previous Vivox versions to version 16.0.0.
Read time 7 minutesLast updated 7 months ago
This guide provides details on how to upgrade to Vivox Unity SDK version 16.0.0 and highlights the differences between the two SDK versions.
Version 16.0.0 introduces simplified functionalities and requires changes in areas related too:
- SDK Initialization
- Login / Logout
- Blocking Players
- Connecting to Channels
- Channel participant management
- Audio device management
- Setting active device
- Setting device gain/volume
- Device muting
SDK setup and initialization
Replace with to access Vivox APIs.
using VivoxUnity;using Unity.Services.Vivox;The VivoxUnity namespace no longer exists.
Additionally, the Client, which was the original way to initialize the SDK, no longer exists.
Using Authentication
If you are using the Unity Authentication package, first, followed by an , before calling , as shown in the following example:
await UnityServices.InitializeAsync()await AuthenticationService.Instance.SignInAnonymously()await VivoxService.Instance.InitializeAsync()async void InitializeAsync(){ await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); await VivoxService.Instance.InitializeAsync();}
You can also use any of the other sign in options provided by the as well.
AuthenticationService.InstanceIf the project uses Unity Dashboard credentials but does not use the AuthenticationService package, you can enable Test Mode and omit the sign-in call.
To turn on Test Mode, in the Unity Editor, you can go to Project Settings > Services > Vivox and check the box labeled Test Mode.
AuthenticationServiceThe following example shows the initialization setup without the AuthenticationService package and with Test Mode enabled:
async void InitializeAsync(){ await UnityServices.InitializeAsync(); await VivoxService.Instance.InitializeAsync();}
Using Vivox Developer Portal credentials
If you have legacy Vivox Developer Portal credentials, provide these credentials to through . These credentials can include the signing key for testing purposes.
VivoxService.Instance.InitializeAsyncInitializationOptionsThe following example shows the initialization setup with Vivox Developer Portal credentials:
async void InitializeAsync(){ InitializationOptions options = new InitalizationOptions(); // _key can be excluded from SetVivoxCredentials if an implementation of IVivoxTokenProvider has been provided to the SDK. options.SetVivoxCredentials(_server, _domain, _issuer, _key); await UnityServices.InitializeAsync(options); await VivoxService.Instance.InitializeAsync();}
Login lifecycle changes
ILoginSession.BeginLoginVivoxService.Instance.LoginAsync(LoginOptions options = null)LoginOptions- DisplayName
- Enabling text-to-speech
- Disabling automatic channel transmission swapping when joining a new channel
- Caching a list of PlayerIDs to be blocked (cross-muted) for the current player upon logging in
- Setting the rate at which the SDK receives participant update events
async void LoginToVivoxAsync(List<string> blockedList, string displayName){ LoginOptions options = new LoginOptions(); options.DisplayName = displayName; options.BlockedUserList = blockedList; await VivoxService.Instance.LoginAsync(options);}
ILoginSession.Logout()VivoxService.Instance.LogoutAsyncMove any logic tied to a event with the PropertyName "State" and either the or to a different function. Split out the logic for login and logout to trigger on and separately, as shown in the following example.
LoginSession.PropertyChangedLoginState.LoggedInLoginState.LoggedOutVivoxService.Instance.LoggedInVivoxService.Instance.LoggedOutvoid Start(){ VivoxService.Instance.LoggedIn += OnUserLoggedIn; VivoxService.Instance.LoggedOut += OnUserLoggedOut;}void OnDestroy(){ //Cleanup events if the GameObject is destroyed to prevent unexpected behavior. VivoxService.Instance.LoggedIn -= OnUserLoggedIn; VivoxService.Instance.LoggedOut -= OnUserLoggedOut;}void OnUserLoggedIn(){ Debug.Log("The user has successfully logged in"); // Other logic such as attempting a default channel join or changing the state of voice UIs to indicate the character is logged in can be done here.}void OnUserLoggedOut(){ Debug.Log("The user has logged out"); // The LoggedOut event will fire after a VivoxService.Instance.LogoutAsync call has been completed successfully // or after an internet disruption has caused Automatic Connection Recovery to declare the attempted recovery a failure.}
Blocking players
ILoginSession.SetCrossMutedCommunicationsVivoxServince.Instance.BlockPlayerAsync(string playerId)VivoxService.Instance.UnblockPlayerAsync(string playerId)playerIdAccountIdChannel lifecycle changes
Joining and leaving channels
All logic related to has been replaced by the following methods:
ChannelSession.BeginConnectJoinGroupChannelAsync(string channelName, ChatCapability chatCapability, ChannelOptions channelOptions = null);JoinPositionalChannelAsync(string channelName, ChatCapability chatCapability, Channel3DProperties positionalChannelProperties, ChannelOptions channelOptions = null);JoinEchoChannelAsync(string channelName, ChatCapability chatCapability, ChannelOptions channelOptions = null);
ChannelSession.Disconnect()- to leave a specific channel.
VivoxService.Instance.LeaveChannelAsync(string channelName) - to leave all active channels. We recommend using this method if only a single channel is joined at any time.
VivoxService.Instance.LeaveAllChannelsAsync()
ILoginSession.ChannelSessionsVivoxService.Instance.ActiveChannelsVivoxService.Instance.ActiveChannelsVivoxParticipantsImplementations previously depended on and to determine if the user was connected to the channel. Replace events tracking state changes of those two properties with functions tied to , and .
AudioStateTextStateVivoxService.Instance.ChannelJoinedVivoxService.Instance.ChannelLeftVivoxService.Instance.ChannelJoinedVivoxService.Instance.ChannelLeftThe follow code sample is an example of these changes:
void BindToChannelActions(){ //Connecting/Disconnecting channel actions can be done upon VivoxService.Instance.LoggedIn and LoggedOut events being fired, respectively, //for basic lifecycle control VivoxService.Instance.ChannelJoined += OnChannelJoined; VivoxService.Instance.ChannelLeft += OnChannelLeft;}void UnbindChannelActions(){ VivoxService.Instance.ChannelJoined -= OnChannelJoined; VivoxService.Instance.ChannelLeft -= OnChannelLeft;}void OnChannelJoined(string channelName){ //The channel with name channelName has been successfully joined, and any UI updates should be done for it //Additionally, if multiple channels are being handled, keeping track of the channelName and knowing which channel is for which purpose should be done //on the side of the implementation based on this event.}void OnChannelLeft(string channelName){ //The channel with name channelName has been successfully left, and any UI updates should be done for it //This also means that the channelName can likely be safely lost, although if the channelName is specific and might be rejoined, //it should be held on to by the implementation.}
Channel transmission
By default, Channel transmission (the channel being spoken into) switches to the most recently joined channel. You can disable this by setting to true before calling with that LoginOptions object as input.
LoginOptions.DisableAutomaticChannelTransmissionSwapVivoxService.Instance.LoginAsyncYou can change the channel transmission mode manually by using the method.
VivoxService.Instance.SetChannelTransmissionModeAsync(TransmissionMode transmissionMode, string channelName = null)This API can set a specific channel, all channels, or no channels as the transmitting channel(s) based on the method’s input.
Channel participant management
VivoxParticipant has replaced ChannelParticipant.
You can access a collection of each active channel’s VivoxParticipant collection by retrieving the value from a index provided a correct key (channel name) is used.
VivoxService.Instance.ActiveChannelsUse and to know when a VivoxParticipant was added or removed from a channel.
VivoxService.Instance.ParticipantAddedToChannelVivoxServince.Instance.ParticipantRemovedFromChannelThe events will provide the relevant VivoxParticipant as input to any callbacks bound to them. The VivoxParticipant object contains information about the PlayerId of the participant, the name of the channel the participant joined or left, and other valuable information, including whether the participant is the local player or their display name. These types of events are shown in the following examples:
void BindToParticipantEvents(){ //These events can be connected at the same time as the channel events from the previous sections for convenience purposes VivoxService.Instance.ParticipantAddedToChannel += OnParticipantAdded; VivoxService.Instance.ParticipantRemovedFromChannel += OnParticipantRemoved;}void UnbindParticipantEvents(){ VivoxService.Instance.ParticipantAddedToChannel -= OnParticipantAdded; VivoxService.Instance.ParticipantRemovedFromChannel -=OnParticipantRemoved;}void OnParticipantAdded(VivoxParticipant participant){ // Use VivoxParticipant to establish some UI element tied to that specific VivoxParticipant, which can then be used to control things such as muting // volume, and blocking, along with UI that can track certain states - such as whether the player is currently muted or is currently speaking.}void OnParticipantRemoved(VivoxParticipant participant){ // At this point, any UI created to display information about this VivoxParticipant can be safely disposed of.}
VivoxParticipant objects
VivoxParticipant- , which states if the participant represents the local player.
VivoxParticipant.IsSelf - , which states if the participant's audio energy passed the threshold to be considered speech by the Vivox SDK.
VivoxParticipant.SpeechDetected
VivoxParticipant also contains the events:
ParticipantMuteStateChangedParticipantSpeechDetectedParticipantAudioEnergyChanged
You can use these events to create a functional volume unit (VU) meter or speech indicator.
Connect these events at the individual UI level for each participant.
The following sample class is a simplified version of the RosterItem in the ChatChannelSample:
public class RosterItem : MonoBehaviour{ public VivoxParticipant Participant; public Text PlayerNameText; public void SetupRosterItem(VivoxParticipant participant) { Participant = participant; PlayerNameText.text = Participant.DisplayName; Participant.ParticipantMuteStateChanged += OnMuteStateChanged; Participant.ParticipantSpeechDetected += OnSpeechDetected; } void OnMuteStateChanged() { if(Participant.IsMuted) { //Participant has been muted } else { //Participant has been unmuted } } void OnSpeechDetected() { //Participant AudioEnergy has exceeded the value required to be considered speech }}
Muting channel participants
VivoxParticipant.MutePlayerLocally()VivoxParticipant.UnmutePlayerLocally()Audio device management
Device management is no longer achieved by leveraging the Client object. You can now make any device adjustments such as muting, changing the volume, or switching the active device using the .
VivoxService.InstanceMuting the local player
Muting yourself locally has changed to utilize the functions and instead of having to do this through APIs on an IAudioDevice object, or switching the channel transmission to none.
These APIs will mute or unmute the local user’s input device.
VivoxService.Instance.MuteInputDevice()VivoxService.Instance.UnmuteInputDevice()Adjusting input/output device volume
You can now adjust device volume by using and
.
VivoxService.Instance.SetInputDeviceVolume(int value)VivoxService.Instance.SetOutputDeviceVolume(int value)Changing the active input/output device
Adjusting the active input or output device can be achieved by calling either or
.
VivoxService.Instance.SetActiveOutputDeviceAsync(VivoxOutputDevice device)VivoxService.Instance.SetActiveInputDeviceAsync(VivoxInputDevice device)A collection of and can be retrieved by accessing the properties and .
VivoxInputDevicesVivoxOutputDevicesVivoxService.Instance.AvailableInputDevicesVivoxService.Instance.AvailableOututDevicesThe following example demonstrates how to use the Vivox SDK audio device APIs to populate a dropdown list of input devices that provides functionality for changing the active input device.
using System.Linq;using Unity.Services.Vivox;using UnityEngine;using UnityEngine.UI;public class AudioDeviceSettings : MonoBehaviour{ public Dropdown InputDeviceDropdown; private void Start() { VivoxService.Instance.AvailableInputDevicesChanged += RefreshInputDeviceList; InputDeviceDropdown.onValueChanged.AddListener((i) => { InputDeviceValueChanged(i); }); } void OnEnable() { RefreshInputDeviceList(); } void OnDestroy() { // Unbind all UI actions InputDeviceDropdown.onValueChanged.RemoveAllListeners(); VivoxService.Instance.AvailableInputDevicesChanged -= RefreshInputDeviceList; } private void RefreshInputDeviceList() { InputDeviceDropdown.Hide(); InputDeviceDropdown.ClearOptions(); InputDeviceDropdown.options.AddRange(VivoxService.Instance.AvailableInputDevices.Select(v => new Dropdown.OptionData() { text = v.DeviceName })); InputDeviceDropdown.SetValueWithoutNotify(InputDeviceDropdown.options.FindIndex(option => option.text == VivoxService.Instance.ActiveInputDevice.DeviceName)); InputDeviceDropdown.RefreshShownValue(); } void InputDeviceValueChanged(int index) { VivoxService.Instance.SetActiveInputDeviceAsync(VivoxService.Instance.AvailableInputDevices.Where(device => device.DeviceName == InputDeviceDropdown.options[index].text).First()); }}