Vivox Unity SDK 快速入门指南
Follow this workflow to get started with Vivox voice and text chat.
阅读时间7 分钟最后更新于 13 天前
Unity 提供 Vivox 服务,作为游戏开发者通过频道式工作流程在用户之间提供语音和文本通信的一种方式,无需投资实现自托管解决方案。 Unity 用于多人游戏通信的语音和文本聊天服务通过管理的托管解决方案提供语音聊天和直接消息文本服务。 将插件添加到游戏中,配置项目设置以立即为项目添加通信功能。在 2D 和 3D 频道中连接无限数量的用户。允许用户控制音量、执行静音操作和管理频道。让用户加入团队聊天。允许用户参与多个频道。
在 Unity 项目中启用 Vivox 服务
要在 Unity Dashboard 上为 Unity 项目设置 Vivox 语音和文本,请完成以下步骤。- 在 Unity Dashboard https://cloud.unity3d.com 上创建帐户和项目。
- 在左侧面板中,选择 Products。在 Communication & Safety 下,选择 Vivox Voice and Text Chat。
- 遵循入门步骤。在入门过程中,您将获得 Vivox API 凭据以在项目中使用。
将您的 Unity 项目连接到 Vivox
要将 Unity 项目连接到 Vivox,请完成以下步骤。- 创建或打开本地 Unity 项目。
- 在编辑器中,选择 Window > Package Manager > Search Unity Registry。
- 搜索 Vivox。选择 Install。
- 选择 Edit > Project Settings > Services,确保项目有与之关联的正确 Project ID。
-
在 Service 下,选择 Vivox,并确保已将正确的凭据拉入项目中。
生命周期操作
Initialize
使用 Unity Authentication 服务时,Vivox 包会为您处理凭据和令牌。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();}
登录
初始化 Vivox SDK 后,可以使用 VivoxService 进行登录。using Unity.Services.Vivox;public async void LoginToVivoxAsync(){ LoginOptions options = new LoginOptions(); options.DisplayName = UserDisplayName; options.EnableTTS = true; await VivoxService.Instance.LoginAsync(options);}
加入回声频道
登录后,使用VivoxService.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);}
确认可以在回声频道中听到自己的声音后,加入位置频道或群组频道即可听到其他参与者的声音。
VivoxService.Instance.JoinGroupChannelAsync((string channelName, ChatCapability chatCapability, ChannelOptions channelOptions = null))VivoxService.Instance.JoinPositionalChannelAsync(string channelName, ChatCapability chatCapability, Channel3DProperties positionalChannelProperties, ChannelOptions channelOptions = null)离开语音和文本频道
要从频道中移除用户,请使用VivoxService.Instance.LeaveChannelAsync(string channelName)VivoxService.Instance.LeaveAllChannels()要了解更多信息,请参阅离开频道。using Unity.Services.Vivox;public async void LeaveEchoChannelAsync(){ string channelToLeave = "Lobby"; await VivoxService.Instance.LeaveChannelAsync(channelToLeave);}
注销用户
要注销用户,请调用VivoxService.Instance.LogoutAsync()要了解更多信息,请参阅从游戏注销。using Unity.Services.Vivox;public async void LogoutOfVivoxAsync (){ VivoxService.Instance.LogoutAsync;}
频道内操作
处理参与者事件
每当有新玩家添加到用户所属的频道或从该频道中移除玩家时,Vivox SDK 都会发布事件。可以使用以下形式订阅这些事件: 此事件将使用VivoxParticipantDisplayNamePlayerIdAudioEnergySpeechDetected, IsMutedudioEnergyVivoxParticipant.ParticipantMuteStateChangedVivoxParticipant.ParticipantSpeechDetectedVivoxParticipant.ParticipantAudioEnergyChanged
以下代码片段进一步详细说明了using 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);}
RosterItem要了解更多信息,请参阅参与者事件。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 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; } } }}
向频道发送文本消息
如果游戏加入了启用了文本的频道,则用户可以发送和接收群组文本消息。为了发送消息,游戏会使用VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)ChannelMessageReceivedVivoxMessage要了解更多信息,请参阅发送和接收群组文本消息。using 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;}
使其他参与者静音
可以使用本地静音使其他用户对特定用户静音。VivoxParticipant.MutePlayerLocally()VivoxParticipant.UnmutePlayerLocally()要了解更多信息,请参阅使其他用户对特定用户静音。{if (participant.InAudio && setMute){ participant.MutePlayerLocally}else if (participant.InAudio){ participant.UnmutePlayerLocally}else{ //Tell player to try again Debug.Log("Try Again");}}
使自己静音
可通过两种方法使用户的麦克风静音:AudioInputDevicesSetTransmissionMode()Transmission.None要了解更多信息,请参阅使用户的麦克风静音。await VivoxService.Instance.SetChannelTransmissionModeAsync(Transmission.None, channelName)