Vivox Unity SDK quickstart guide
Follow this workflow to get started with Vivox voice and text chat.
Read time 5 minutesLast updated 2 days ago
Unity offers the Vivox service as a way for game developers to provide voice and text communications between users through a channel-style workflow without the need to invest in a self-hosted solution. Unity's Voice and Text chat service for multiplayer communication offers a voice chat and direct message text service with a managed hosted solution. Plug in to your game and configure your project settings to immediately add communications to your project. Connect an unlimited number of users in 2D and 3D channels. Allow users to control voice volume, perform mute actions, and manage channels. Place users in team chat. Allow users to participate in multiple channels.
Enable the Vivox service in a Unity project
To set up Vivox Voice and Text for a Unity project on the Unity Dashboard, complete the following steps.- Create an account and project on the Unity Dashboard: https://cloud.unity3d.com.
- On the left pane, select Products. Under Communication & Safety, select Vivox Voice and Text Chat.
- Follow the onboarding steps. During the onboarding process, you are provided with Vivox API credentials to use in your project.
Connect your Unity project to Vivox
To connect your Unity project to Vivox, complete the following steps.- Create or open your local Unity project.
- In the Editor, select Window > Package Manager > Search Unity Registry.
- Search for Vivox. Select Install.
- Select Edit > Project Settings > Services and ensure that your project has the correct project ID associated with it.
-
Under Service, select Vivox, and ensure that the correct credentials have been pulled into the project.
Lifecycle actions
Initialize
When using Unity Authentication Services, the Vivox package handles credentials and tokens for you.using System;using UnityEngine;using Unity.Services.Authentication;using Unity.Services.Core;using Unity.Services.Vivox;async void InitializeAsync(){ await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); await VivoxService.Instance.InitializeAsync();}
Sign in
After you initialize the Vivox SDK, the VivoxService can be used to log in.using Unity.Services.Vivox;public async void LoginToVivoxAsync(){ LoginOptions options = new LoginOptions(); options.DisplayName = UserDisplayName; options.EnableTTS = true; await VivoxService.Instance.LoginAsync(options);}
Join an echo channel
After logging in, useVivoxService.Instance.JoinEchoChannelAsync(string channelName, ChatCapability chatCapability, ChannelOptions channelOptions = null)using Unity.Services.Vivox;public async void JoinEchoChannelAsync(){ string channelToJoin = "Lobby"; await VivoxService.Instance.JoinEchoChannelAsync(channelToJoin, ChatCapability.TextAndAudio);}
After confirming that you can hear your voice in an echo channel, join a positional or group channel to hear other participants.
- Use to join a group channel.
VivoxService.Instance.JoinGroupChannelAsync((string channelName, ChatCapability chatCapability, ChannelOptions channelOptions = null)) - Use to join a positional channel.
VivoxService.Instance.JoinPositionalChannelAsync(string channelName, ChatCapability chatCapability, Channel3DProperties positionalChannelProperties, ChannelOptions channelOptions = null)
Leave a voice and text channel
To remove a user from the channel useVivoxService.Instance.LeaveChannelAsync(string channelName)VivoxService.Instance.LeaveAllChannelsAsync()For more information, refer to Leave a channel.using Unity.Services.Vivox;public async void LeaveEchoChannelAsync(){ string channelToLeave = "Lobby"; await VivoxService.Instance.LeaveChannelAsync(channelToLeave);}
Sign out a user
To sign out a user, call theVivoxService.Instance.LogoutAsync()For more information, refer to Sign out of a game.using Unity.Services.Vivox;public async void LogoutOfVivoxAsync (){ await VivoxService.Instance.LogoutAsync();}
In-channel actions
Handle participant events
The Vivox SDK posts events whenever a new player is added to or removed from a channel the user is a part of. These events can be subscribed to as: This event will fire with an object of typeVivoxParticipantDisplayNamePlayerIdAudioEnergySpeechDetected, IsMutedudioEnergyVivoxParticipant.ParticipantMuteStateChangedVivoxParticipant.ParticipantSpeechDetectedVivoxParticipant.ParticipantAudioEnergyChanged
The following code snippet further details the structureusing Unity.Service.Vivox;List<RosterItem> rosterList = new List<RosterItem>();private void BindSessionEvents(bool doBind){ if(doBind) { VivoxService.Instance.ParticipantAddedToChannel += onParticipantAddedToChannel; VivoxService.Instance.ParticipantRemovedFromChannel += onParticipantRemovedFromChannel; } else { VivoxService.Instance.ParticipantAddedToChannel -= onParticipantAddedToChannel; VivoxService.Instance.ParticipantRemovedFromChannel -= onParticipantRemovedFromChannel; }}private void onParticipantAddedToChannel(VivoxParticipant participant){ ///RosterItem is a class intended to store the participant object, and reflect events relating to it into the game's UI. ///It is a sample of one way to use these events, and is detailed just below this snippet. RosterItem newRosterItem = new RosterItem(); newRosterItem.SetupRosterItem(participant); rosterList.Add(newRosterItem);}private void onParticipantRemovedFromChannel(VivoxParticipant participant){ RosterItem rosterItemToRemove = rosterList.FirstOrDefault(p => p.Participant.PlayerId == participant.PlayerId); rosterList.Remove(rosterItemToRemove);}
RosterItemFor more information, refer to Participant events.using Unity.Service.Vivox;using UnityEngine;public class RosterItem : MonoBehaviour{ public VivoxParticipant Participant; public Text PlayerNameText; public Image ChatStateImage; public Sprite MutedImage; public Sprite SpeakingImage; public Sprite NotSpeakingImage; public void SetupRosterItem(VivoxParticipant participant) { Participant = participant; PlayerNameText.text = Participant.DisplayName; UpdateChatStateImage(); Participant.ParticipantMuteStateChanged += UpdateChatStateImage; Participant.ParticipantSpeechDetected += UpdateChatStateImage; } private void UpdateChatStateImage() { /// Update the UI of the game to the state of the participant if (Participant.IsMuted) { ChatStateImage.sprite = MutedImage; ChatStateImage.gameObject.transform.localScale = Vector3.one; } else { if (Participant.SpeechDetected) { ChatStateImage.sprite = SpeakingImage; ChatStateImage.gameObject.transform.localScale = Vector3.one; } else { ChatStateImage.sprite = NotSpeakingImage; } } }}
Send text messages to a channel
If the game has joined a channel with text enabled, users can send and receive group text messages. To send messages, the game uses theVivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)ChannelMessageReceivedVivoxMessageFor more information, refer to Send and receive group text messages. For information on sending direct messages, refer to Directed messagesusing Unity.Service.Vivox;private async void SendMessageAsync(string channelName, string message){ VivoxService.Instance.SendChannelTextMessageAsync(channelName, message);}private void BindSessionEvents(bool doBind){ VivoxService.Instance.ChannelMessageReceived += onChannelMessageReceived;}private void onChannelMessageReceived(VivoxMessage message){ string messageText = message.MessageText; string senderID = message.SenderPlayerId; string senderDisplayName = message.SenderDisplayName; string messageChannel = message.ChannelName;}
Mute other participants
You can mute other users for a specific user by using a local mute. TheVivoxParticipant.MutePlayerLocally()VivoxParticipant.UnmutePlayerLocally()For more information, refer to Mute other users for a specific user.{if (participant.InAudio && setMute){ participant.MutePlayerLocally}else if (participant.InAudio){ participant.UnmutePlayerLocally}else{ //Tell player to try again Debug.Log("Try Again");}}
Mute yourself
There are two methods for muting a user’s microphone:AudioInputDevicesSetTransmissionMode()Transmission.NoneFor more information, refer to Mute a user's microphone.await VivoxService.Instance.SetChannelTransmissionModeAsync(Transmission.None, channelName)
Next steps
Once basic chat is working you can expand on Vivox functionality with additional tools:- Add features to enrich text chat.
- Consider accessibility features such as speech-to-text.
- Review the Developer guide for more topics and features.