docs.unity3d.com
    Show / Hide Table of Contents

    Manage voice chat

    You can use the Vivox voice chat service to integrate voice channels in your rooms and facilitate collaboration between participants.

    Prerequisites

    Before you manage your voice chat, you must first manage your room.

    Instantiate the Vivox voice chat service

    To instantiate the Vivox service, see the following steps:

    1. Go to your PlatformServices script in your Assets/Scripts directory.
    2. In your PlatformServices script file, update the PlatformServices class with a public reference to IVoiceService and a private reference to VivoxService.

       static VivoxService s_VivoxService;
      
       public static IVoiceService VoiceService => s_VivoxService;
      
    3. Initialize the services in the InitializeAsync method. The variables provided in the constructor are defined in get started and in the Identity authentication guide.

       public static async Task InitializeAsync()
       {
           // ...
      
           s_VivoxService = new VivoxService(s_ServiceHttpClient, s_PresenceManager, s_CloudConfiguration);
      
           // ...
       }
      
    4. Shut down the services in the Shutdown method.

       public static void Shutdown()
       {
           // ...
      
           s_VivoxService.Dispose();
           s_VivoxService = null;
      
           // ...
       }
      

    Join and leave voice channels

    To join or leave a voice channel, see the following steps:

    1. In your scene, create Join Voice and Leave Voice buttons.
      Screenshots of buttons for the voice chat
    2. Go to GameObject > Create Empty and name the new game object VoiceManager.
    3. In your Assets/Scripts directory, create a new script and name it VoiceManager.
    4. Attach the VoiceManager script to your VoiceManager game object.
    5. In your PresentationManager script file, use the PresentationManager class to do the following:

      • Reference an IVoiceService and an ISessionProvider
      • Reference the join and leave buttons
      • Store the current session
      • Describe whether the participant has joined the voice channel

        public class VoiceManager : MonoBehaviour
        {
          [SerializeField]
          Button m_JoinButton;
        
          [SerializeField]
          Button m_LeaveButton;
        
          ISessionProvider m_SessionProvider;
          IVoiceService m_VoiceService;
        
          ISession m_CurrentSession;
          bool m_HasJoined;
        }
        
    6. Use the Awake method to retrieve services from PlatformServices and to subscribe to the SessionChanged event and the button clicks. Use the OnDestroy method to unsubscribe from the events.

       void Awake()
       {
           m_SessionProvider = PlatformServices.SessionProvider;
           m_VoiceService = PlatformServices.VoiceService;
      
           m_SessionProvider.SessionChanged += OnSessionChanged;
      
           m_JoinButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnJoinClicked));
           m_LeaveButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnLeaveClicked));
       }
      
       void OnDestroy()
       {
           m_SessionProvider.SessionChanged -= OnSessionChanged;
      
           m_JoinButton.onClick.RemoveAllListeners();
           m_LeaveButton.onClick.RemoveAllListeners();
       }
      
    7. Use the OnSessionChanged method to clean up any previous sessions and to make sure the buttons are displayed correctly.

       async void OnSessionChanged(ISession session)
       {
           m_JoinButton.interactable = false;
           m_LeaveButton.interactable = false;
      
           // Clean up any previous session
           if (m_CurrentSession != null && m_HasJoined)
           {
               await m_VoiceService.LeaveAsync();
           }
      
           m_CurrentSession = session;
           if (session != null)
           {
               m_JoinButton.interactable = true;
           }
       }
      
    8. Use the OnJoinClicked method to implement callbacks for the button clicks.

       async void OnJoinClicked()
       {
           m_JoinButton.interactable = false;
      
           await m_VoiceService.JoinAsync();
      
           m_HasJoined = true;
           m_LeaveButton.interactable = true;
       }
      
       async void OnLeaveClicked()
       {
           m_LeaveButton.interactable = false;
      
           await m_VoiceService.LeaveAsync();
      
           m_HasJoined = false;
           m_JoinButton.interactable = true;
       }
      

    (Optional)Display information about a voice channel

    To display information about a voice channel, see the following steps:

    1. In your scene, create a text field to display information about the voice channel.
    2. In your Assets/Scripts directory, create a new script and name it VoiceTextUpdater.
    3. Attach the VoiceTextUpdater script to your VoiceManager game object.
    4. In your VoiceTextUpdater script file, use the VoiceTextUpdater class to reference an IVoiceService and your text field.

       public class VoiceTextUpdater : MonoBehaviour
       {
           [SerializeField]
           Text m_TextField;
      
           IVoiceService m_VoiceService;
       }
      
    5. Use the Awake method to retrieve services from PlatformServices and to subscribe to the VoiceUpdated event. Use the OnDestroy method to unsubscribe from the event.

       void Awake()
       {
           m_VoiceService = PlatformServices.VoiceService;
           m_VoiceService.VoiceUpdated += ApplyVoiceUpdate;
       }
      
       void OnDestroy()
       {
           m_VoiceService.VoiceUpdated -= ApplyVoiceUpdate;
       }
      
    6. Use the ApplyVoiceUpdate method to update the text with information about the voice chat.

       void ApplyVoiceUpdate(IEnumerable<VoiceParticipant> voiceParticipants)
       {
           if (voiceParticipants != null && voiceParticipants.Any())
           {
               var sb = new StringBuilder();
               foreach (var voiceParticipant in voiceParticipants)
               {
                   sb.AppendLine($"{voiceParticipant.DisplayName} : {voiceParticipant.AudioIntensity}");
               }
      
               m_TextField.text = sb.ToString();
           }
           else
           {
               m_TextField.text = "No voice participant in the room.";
           }
       }
      
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023