Vivox Unity SDK 16.0.0 upgrade guide
Upgrade from previous Vivox versions to version 16.0.0.
Read time 7 minutesLast updated 2 days 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
Replaceusing VivoxUnity;using Unity.Services.Vivox;Using Authentication
If you are using the Unity Authentication package,await UnityServices.InitializeAsync()await AuthenticationService.Instance.SignInAnonymously()await VivoxService.Instance.InitializeAsync()You can also use any of the other sign in options provided by theasync void InitializeAsync(){ await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); await VivoxService.Instance.InitializeAsync();}
AuthenticationService.InstanceAuthenticationService
The 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 toVivoxService.Instance.InitializeAsyncInitializationOptionsasync 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.LogoutAsyncLoginSession.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 toChannelSession.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.ActiveChannelsVivoxParticipantsAudioStateTextStateVivoxService.Instance.ChannelJoinedVivoxService.Instance.ChannelLeftVivoxService.Instance.ChannelJoinedVivoxService.Instance.ChannelLeftvoid 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 settingLoginOptions.DisableAutomaticChannelTransmissionSwapVivoxService.Instance.LoginAsyncVivoxService.Instance.SetChannelTransmissionModeAsync(TransmissionMode transmissionMode, string channelName = null)Channel participant management
VivoxParticipant has replaced ChannelParticipant. You can access a collection of each active channel’s VivoxParticipant collection by retrieving the value from aVivoxService.Instance.ActiveChannelsVivoxService.Instance.ParticipantAddedToChannelVivoxServince.Instance.ParticipantRemovedFromChannelvoid 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
ParticipantMuteStateChangedParticipantSpeechDetectedParticipantAudioEnergyChanged
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 theVivoxService.InstanceMuting the local player
Muting yourself locally has changed to utilize the functionsVivoxService.Instance.MuteInputDevice()VivoxService.Instance.UnmuteInputDevice()Adjusting input/output device volume
You can now adjust device volume by usingVivoxService.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 eitherVivoxService.Instance.SetActiveOutputDeviceAsync(VivoxOutputDevice device)VivoxService.Instance.SetActiveInputDeviceAsync(VivoxInputDevice device)VivoxInputDevicesVivoxOutputDevicesVivoxService.Instance.AvailableInputDevicesVivoxService.Instance.AvailableOututDevicesusing 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()); }}