-
Notifications
You must be signed in to change notification settings - Fork 92
/
VivoxManager.cs
84 lines (74 loc) · 2.68 KB
/
VivoxManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Unity.Services.Core;
using Unity.Services.Vivox;
using UnityEngine;
namespace Unity.Megacity.Gameplay
{
/// <summary>
/// Manages Vivox service
/// </summary>
[RequireComponent(typeof(VivoxSession))]
[RequireComponent(typeof(VivoxChannel))]
[RequireComponent(typeof(VivoxDevicesVolume))]
public class VivoxManager : MonoBehaviour
{
public static VivoxManager Instance { get; private set; }
public VivoxDevicesVolume Devices { get; private set; }
public VivoxSession Session { get; private set; }
public VivoxChannel Channel { get; private set; }
private bool m_IsReady;
private void Awake()
{
if (Instance != this && Instance != null)
{
Debug.LogWarning(VivoxEvents.k_MultipleVivoxComponentDetected);
Destroy(this);
return;
}
if (string.IsNullOrEmpty(Application.cloudProjectId))
{
Debug.LogWarning(VivoxEvents.k_SetupProjectInTheCloud);
Destroy(this);
return;
}
Instance = this;
m_IsReady = false;
Devices = GetComponent<VivoxDevicesVolume>();
Session = GetComponent<VivoxSession>();
Channel = GetComponent<VivoxChannel>();
}
private async void Start()
{
// if the Unity project is not linked to a Unity services project.
Debug.Log($"Vivox application project {Application.cloudProjectId}");
if (string.IsNullOrEmpty(Application.cloudProjectId))
{
Debug.LogWarning(VivoxEvents.k_SetupProjectInTheCloud);
return;
}
await UnityServices.InitializeAsync(new InitializationOptions());
if (UnityServices.State == ServicesInitializationState.Initialized && VivoxService.Instance != null)
{
m_IsReady = true;
VivoxService.Instance.Initialize();
}
else
{
Debug.Log($"State {UnityServices.State} VivoxService Instance Is null: {VivoxService.Instance == null}");
}
}
public void Logout()
{
// Needed to add this to prevent some unsuccessful init, we can revisit to do better
if (m_IsReady)
{
Devices.SetMicrophoneMute(true);
Session.ClosingClientConnection();
}
}
private void OnApplicationQuit()
{
if (m_IsReady && Session != null)
Session.ClosingClientConnection();
}
}
}