Vivox Unity SDK クイックスタートガイド
Follow this workflow to get started with Vivox voice and text chat.
読み終わるまでの所要時間 5 分最終更新 23日前
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 (ウィンドウ > パッケージマネージャー > Unity Registry を検索) を選択します。
- Vivox を検索します。Install (インストール) を選択します。
- Edit > Project Settings > Services (編集 > プロジェクト設定 > サービス) を選択し、プロジェクトに正しいプロジェクト ID が関連付けられていることを確認します。
-
Service (サービス) で、Vivox を選択し、正しい認証情報がプロジェクトにプルされていることを確認します。
ライフサイクルのアクション
Initialize
Unity Authentication Services を使用するときは、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)