Vivox 集成
Note: You must be using a Vivox package version of at least 16.0.0 for the integration to work.
Vivox 服务为游戏开发者提供了一种方法,让他们能够在用户之间实现语音和文本通信,而无需投资自托管解决方案。Vivox 通过要求您在用户执行加入频道、静音或踢出其他玩家等授权操作之前对其进行安全身份验证和授权来保护用户。
过去,Vivox 要求客户端实现一个服务器令牌,该令牌为每个授权操作安全地生成 Vivox 访问令牌 (VAT),类似于每个授权操作的 JSON Web 令牌 (JWT)。为每个授权操作生成 VAT 很繁琐,并且通常会对在游戏中安全地实现语音通信构成重大障碍。大厅通过充当连接到大厅的用户的令牌提供者来简化使用 Vivox 安全地实现语音通信。
Note: You must have both the Vivox and the Lobby packages installed to use the two services together. You can install these packages from the Unity Cloud Dashboard. See the Vivox SDK documentation to learn how to set up Vivox.
继续阅读,了解如何使用 Vivox 和大厅的快速指南。
加入语音频道
在您的项目中启用 Vivox 后
使用 Unity 身份验证包 进行身份验证。以下代码示例说明了如何进行身份验证和初始化 Vivox。
C#
using System; using UnityEngine; using Unity.Services.Authentication; using Unity.Services.Core; using Unity.Services.Vivox; using VivoxUnity; async void Start() { // Authenticate with Unity services await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); await VivoxService.Instance.InitializeAsync(); // Log in to Vivox services await VivoxService.Instance.LoginAsync(); }
加入大厅的语音频道。当您尝试加入 Vivox 频道时,它会调用令牌提供者。如果频道 ID 与大厅 ID 相匹配,则会生成一个令牌。
以下代码示例演示了如何创建大厅,然后加入与其关联的语音频道。
C#
using System; using UnityEngine; using Unity.Services.Core; using Unity.Services.Lobbies; using Unity.Services.Lobbies.Models; using Unity.Services.Vivox; using VivoxUnity; async void CreateLobbyAndJoinVoice() { // Create a new lobby (or join an existing one) Lobby myLobby = await LobbyService.Instance.CreateLobbyAsync( lobbyName: "myLobby", maxPlayers: 5, options: new CreateLobbyOptions() { IsPrivate = false, }); // Join the voice channel await VivoxService.Instance.JoinGroupChannelAsync(myLobby.Id, ChatCapability.AudioOnly); }
使用 Vivox 事件来接收有关频道更改的通知。例如,
VivoxService.Instance.ChannelJoined += channelName => { ... }
。
与大厅的其他交互
除了创建或加入大厅之外,您还可以使用 Vivox SDK 与 Vivox 频道交互。但是,某些操作应使用 Lobby SDK 来执行,以保持大厅和语音频道同步。以下列表包含您必须通过 Lobby SDK 执行的示例操作
重要说明:
- 使用
VivoxService.Instance.JoinGroupChannelAsync(...)
加入频道。 - 在加入频道之前,前一个代码示例中的
VivoxService.Instance.JoinGroupChannelAsync(...)
行要求您await VivoxService.Instance.LoginAsync()
- 不支持服务器端静音。您可以通过 Vivox SDK 控制本地静音。