浏览代码

Merge from vivox_polish branch

/main/staging
nathaniel.buck@unity3d.com 3 年前
当前提交
3188678f
共有 8 个文件被更改,包括 116 次插入71 次删除
  1. 2
      Assets/Art/Volume/micIcon.png.meta
  2. 4
      Assets/Prefabs/UI/LobbyUserList.prefab
  3. 4
      Assets/Prefabs/UI/UserCardPanel.prefab
  4. 63
      Assets/Scripts/Game/GameManager.cs
  5. 9
      Assets/Scripts/UI/InLobbyUserUI.cs
  6. 49
      Assets/Scripts/UI/LobbyUserVolumeUI.cs
  7. 39
      Assets/Scripts/Vivox/VivoxSetup.cs
  8. 17
      Assets/Scripts/Vivox/VivoxUserHandler.cs

2
Assets/Art/Volume/micIcon.png.meta


serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0

4
Assets/Prefabs/UI/LobbyUserList.prefab


m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 1
m_PresetInfoIsWorld: 0
--- !u!114 &4463750083940306577
MonoBehaviour:
m_ObjectHideFlags: 0

m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.14320946, g: 1, b: 0, a: 0.392}
m_RaycastTarget: 1
m_RaycastTarget: 0
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:

4
Assets/Prefabs/UI/UserCardPanel.prefab


m_Name:
m_EditorClassIdentifier:
m_volumeSliderContainer: {fileID: 6299655602247130973}
m_muteButtonContainer: {fileID: 6569968321909521185}
m_muteToggleContainer: {fileID: 6569968321909521185}
m_volumeSlider: {fileID: 774196503220678520}
m_muteToggle: {fileID: 6442186483341508885}
--- !u!114 &6299655602247130973
MonoBehaviour:
m_ObjectHideFlags: 0

63
Assets/Scripts/Game/GameManager.cs


using LobbyRelaySample.relay;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Debug.Log("Signed in.");
m_localUser.ID = Locator.Get.Identity.GetSubIdentity(Auth.IIdentityType.Auth).GetContent("id");
m_localUser.DisplayName = NameGenerator.GetName(m_localUser.ID);
m_vivoxSetup.Initialize(m_vivoxUserHandlers); // Should be before AddPlayer, since that will attempt to set the Vivox ID based on the Auth ID.
StartVivoxLogin();
}
private void BeginObservers()

m_lobbyContentHeartbeat.BeginTracking(m_localLobby, m_localUser);
SetUserLobbyState();
StartRelayConnection();
m_vivoxSetup.JoinLobbyChannel(m_localLobby.LobbyID, null); // TODO: Retry on failure?
StartVivoxJoin();
}
private void OnLeftLobby()

SetGameState(GameState.JoinMenu);
}
private void StartVivoxLogin()
{
m_vivoxSetup.Initialize(m_vivoxUserHandlers, OnVivoxLoginComplete);
void OnVivoxLoginComplete(bool didSucceed)
{
if (!didSucceed)
{ Debug.LogError("Vivox login failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartVivoxLogin, m_localLobby.LobbyID));
return;
}
}
}
private void StartVivoxJoin()
{
m_vivoxSetup.JoinLobbyChannel(m_localLobby.LobbyID, OnVivoxJoinComplete);
void OnVivoxJoinComplete(bool didSucceed)
{
if (!didSucceed)
{ Debug.LogError("Vivox connection failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartVivoxJoin, m_localLobby.LobbyID));
return;
}
}
}
private void StartRelayConnection()
{
if (m_localUser.IsHost)

OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Connecting);
m_relaySetup.BeginRelayJoin(m_localLobby, m_localUser, OnRelayConnected);
}
private void OnRelayConnected(bool didSucceed, RelayUtpClient client)
{
Component.Destroy(m_relaySetup);
m_relaySetup = null;
void OnRelayConnected(bool didSucceed, RelayUtpClient client)
{
Component.Destroy(m_relaySetup);
m_relaySetup = null;
if (!didSucceed)
{ Debug.LogError("Relay connection failed! Retrying in 5s...");
StartCoroutine(RetryConnection(StartRelayConnection, m_localLobby.LobbyID));
return;
}
if (!didSucceed)
{
Debug.LogError("Relay connection failed! Retrying in 5s...");
StartCoroutine(RetryRelayConnection());
return;
m_relayClient = client;
OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Lobby);
m_relayClient = client;
OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Lobby);
private IEnumerator RetryRelayConnection()
private IEnumerator RetryConnection(Action doConnection, string lobbyId)
StartRelayConnection();
if (m_localLobby != null && m_localLobby.LobbyID == lobbyId && !string.IsNullOrEmpty(lobbyId)) // Ensure we didn't leave the lobby during this waiting period.
doConnection?.Invoke();
}
private void BeginCountDown()

9
Assets/Scripts/UI/InLobbyUserUI.cs


m_StatusText.SetText(SetStatusFancy(observed.UserStatus));
m_EmoteText.SetText(observed.Emote.GetString());
m_HostIcon.enabled = observed.IsHost;
SetAudioState(true);
}
/// <summary>
/// Disable or show the Volume Icons in sync with the package.
/// </summary>
void SetAudioState(bool hasVoice)
{
// TODO: Disable if vivox is not available, per-user.
}
string SetStatusFancy(UserStatus status)

49
Assets/Scripts/UI/LobbyUserVolumeUI.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace LobbyRelaySample.UI
{

private UIPanelBase m_volumeSliderContainer;
[SerializeField]
private UIPanelBase m_muteButtonContainer;
private UIPanelBase m_muteToggleContainer;
[SerializeField]
[Tooltip("This is shown for other players, to mute them.")]
private GameObject m_muteIcon;

public bool IsLocalPlayer { private get; set; }
public void EnableVoice()
[SerializeField]
private Slider m_volumeSlider;
[SerializeField]
private Toggle m_muteToggle;
/// <param name="shouldResetUi">
/// When the user is being added, we want the UI to reset to the default values.
/// (We don't do this if the user is already in the lobby so that the previous values are retained. E.g. If they're too loud and volume was lowered, keep it lowered on reenable.)
/// </param>
public void EnableVoice(bool shouldResetUi)
if (shouldResetUi)
{ m_volumeSlider.SetValueWithoutNotify(vivox.VivoxUserHandler.NormalizedVolumeDefault);
m_muteToggle.SetIsOnWithoutNotify(false);
}
m_muteButtonContainer.Show();
m_muteToggleContainer.Show();
m_muteIcon.SetActive(false);
m_micMuteIcon.SetActive(true);
}

m_muteButtonContainer.Show();
m_muteToggleContainer.Show();
public void DisableVoice()
/// <param name="shouldResetUi">
/// When the user leaves the lobby (but not if they just lose voice access for some reason, e.g. device disconnect), reset state to the default values.
/// (We can't just do this during Enable since it could cause Vivox to have a state conflict during participant add.)
/// </param>
public void DisableVoice(bool shouldResetUi)
if (shouldResetUi)
{ m_volumeSlider.value = vivox.VivoxUserHandler.NormalizedVolumeDefault;
m_muteToggle.isOn = false;
}
m_muteButtonContainer.Hide(0.4f);
m_muteToggleContainer.Hide(0.4f);
/* TODO : If we can hook in the volume from a user, we can plug it in here.
/// <summary>
/// Controls the visibility of the volume rings to show activity levels of the voice channel on this user.
/// </summary>
/// <param name="normalizedVolume"></param>
public void OnSoundDetected(float normalizedVolume)
{
m_voiceRings.alpha = normalizedVolume;
}
*/
}
}

39
Assets/Scripts/Vivox/VivoxSetup.cs


/// <summary>
/// Initialize the Vivox service, before actually joining any audio channels.
/// </summary>
public void Initialize(List<VivoxUserHandler> userHandlers)
/// <param name="onComplete">Called whether the login succeeds or not.</param>
public void Initialize(List<VivoxUserHandler> userHandlers, Action<bool> onComplete)
{
if (m_isMidInitialize)
return;

m_loginSession = VivoxService.Instance.Client.GetLoginSession(account);
string token = m_loginSession.GetLoginToken();
m_loginSession.BeginLogin(m_loginSession.GetLoginToken(), SubscriptionMode.Accept, null, null, null, result =>
m_loginSession.BeginLogin(token, SubscriptionMode.Accept, null, null, null, result =>
onComplete?.Invoke(true);
}
catch (Exception ex)
{ UnityEngine.Debug.LogWarning("Vivox failed to login: " + ex.Message);
onComplete?.Invoke(false);
}
finally
{

m_channelSession = m_loginSession.GetChannelSession(channel);
string token = m_channelSession.GetConnectToken();
m_channelSession.BeginConnect(true, false, true, m_channelSession.GetConnectToken(), result =>
m_channelSession.BeginConnect(true, false, true, token, result =>
m_channelSession.EndConnect(result); // TODO: Error handling?
onComplete?.Invoke(true);
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelJoined(m_channelSession);
try
{ m_channelSession.EndConnect(result);
onComplete?.Invoke(true);
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelJoined(m_channelSession);
}
catch (Exception ex)
{ UnityEngine.Debug.LogWarning("Vivox failed to connect: " + ex.Message);
onComplete?.Invoke(false);
}
// TODO: Reset slider and mute UI on lobby enter
/// <summary>
/// To be called when leaving a lobby.

ChannelId id = m_channelSession.Channel;
m_channelSession?.Disconnect(
(result) => { m_loginSession.DeleteChannelSession(id); }); // TODO: What about if this is called while also trying to connect?
if (m_channelSession != null)
{
ChannelId id = m_channelSession.Channel;
m_channelSession?.Disconnect(
(result) => { m_loginSession.DeleteChannelSession(id); m_channelSession = null; });
}
foreach (VivoxUserHandler userHandler in m_userHandlers)
userHandler.OnChannelLeft();
}

/// </summary>
public void Uninitialize()
{
// TODO: Also call LeaveLobbyChannel?
if (!m_hasInitialized)
return;
m_loginSession.Logout();

17
Assets/Scripts/Vivox/VivoxUserHandler.cs


private string m_vivoxId;
private const int k_volumeMin = -50, k_volumeMax = 20; // From the Vivox docs, the valid range is [-50, 50] but anything above 25 risks being painfully loud.
public static float NormalizedVolumeDefault { get { return (0f - k_volumeMin) / (k_volumeMax - k_volumeMin); } }
m_lobbyUserVolumeUI.DisableVoice();
m_lobbyUserVolumeUI.DisableVoice(true);
}
public void SetId(string id)

{
m_vivoxId = participant.Key;
m_lobbyUserVolumeUI.IsLocalPlayer = participant.IsSelf;
m_lobbyUserVolumeUI.EnableVoice();
m_lobbyUserVolumeUI.EnableVoice(true);
break;
}
}

if (isThisUser)
{ m_vivoxId = keyEventArg.Key; // Since we couldn't construct the Vivox ID earlier, retrieve it here.
m_lobbyUserVolumeUI.IsLocalPlayer = participant.IsSelf;
m_lobbyUserVolumeUI.EnableVoice();
m_lobbyUserVolumeUI.EnableVoice(true);
}
}
private void BeforeParticipantRemoved(object sender, KeyEventArg<string> keyEventArg)

bool isThisUser = username == m_id;
if (isThisUser)
{ m_lobbyUserVolumeUI.DisableVoice();
{ m_lobbyUserVolumeUI.DisableVoice(true);
}
}
private void OnParticipantValueUpdated(object sender, ValueEventArg<string, IParticipant> valueEventArg)

if (property == "UnavailableCaptureDevice")
{
if (participant.UnavailableCaptureDevice)
{ m_lobbyUserVolumeUI.DisableVoice();
{ m_lobbyUserVolumeUI.DisableVoice(false);
{ m_lobbyUserVolumeUI.EnableVoice();
{ m_lobbyUserVolumeUI.EnableVoice(false);
participant.SetIsMuteForAll(m_vivoxId, false, null);
}
}

m_lobbyUserVolumeUI.DisableVoice();
m_lobbyUserVolumeUI.DisableVoice(false);
m_lobbyUserVolumeUI.EnableVoice();
m_lobbyUserVolumeUI.EnableVoice(false);
}
}
}

正在加载...
取消
保存