// ----------------------------------------------------------------------------
//
// Photon Voice Demo for PUN - Copyright (C) Exit Games GmbH
//
//
// UI manager class for the PUN Voice Demo
//
// developer@photonengine.com
// ----------------------------------------------------------------------------
using System;
using Photon.Voice.Unity;
using Photon.Voice.PUN;
#pragma warning disable 0649 // Field is never assigned to, and will always have its default value
namespace ExitGames.Demos.DemoPunVoice
{
using Photon.Pun;
using UnityEngine;
using UnityEngine.UI;
using Client.Photon;
#if !UNITY_EDITOR && UNITY_PS4
using Sony.NP;
#elif !UNITY_EDITOR && UNITY_SHARLIN
using System.Runtime.InteropServices;
#endif
public class VoiceDemoUI : MonoBehaviour
{
#if !UNITY_EDITOR && UNITY_SHARLIN
[DllImport("PhotonVoiceLocalUserIDPlugin")]
private static extern int egpvgetLocalUserID(); // returns the user ID of the user at index 0 in the list of local users
#endif
[SerializeField]
private Text punState;
[SerializeField]
private Text voiceState;
private PhotonVoiceNetwork punVoiceNetwork;
private Canvas canvas;
[SerializeField]
private Button punSwitch;
private Text punSwitchText;
[SerializeField]
private Button voiceSwitch;
private Text voiceSwitchText;
[SerializeField]
private Button calibrateButton;
private Text calibrateText;
[SerializeField]
private Text voiceDebugText;
public Recorder recorder;
[SerializeField]
private GameObject inGameSettings;
[SerializeField]
private GameObject globalSettings;
[SerializeField]
private Text devicesInfoText;
private GameObject debugGO;
private bool debugMode;
private float volumeBeforeMute;
private DebugLevel previousDebugLevel;
public bool DebugMode
{
get
{
return this.debugMode;
}
set
{
this.debugMode = value;
this.debugGO.SetActive(this.debugMode);
this.voiceDebugText.text = String.Empty;
if (this.debugMode)
{
this.previousDebugLevel = this.punVoiceNetwork.Client.LoadBalancingPeer.DebugOut;
this.punVoiceNetwork.Client.LoadBalancingPeer.DebugOut = DebugLevel.ALL;
}
else
{
this.punVoiceNetwork.Client.LoadBalancingPeer.DebugOut = this.previousDebugLevel;
}
if (DebugToggled != null)
{
DebugToggled(this.debugMode);
}
}
}
public delegate void OnDebugToggle(bool debugMode);
public static event OnDebugToggle DebugToggled;
[SerializeField]
private int calibrationMilliSeconds = 2000;
private void Awake()
{
this.punVoiceNetwork = PhotonVoiceNetwork.Instance;
}
private void OnEnable()
{
ChangePOV.CameraChanged += this.OnCameraChanged;
BetterToggle.ToggleValueChanged += this.BetterToggle_ToggleValueChanged;
CharacterInstantiation.CharacterInstantiated += this.CharacterInstantiation_CharacterInstantiated;
this.punVoiceNetwork.Client.StateChanged += this.VoiceClientStateChanged;
PhotonNetwork.NetworkingClient.StateChanged += this.PunClientStateChanged;
}
private void OnDisable()
{
ChangePOV.CameraChanged -= this.OnCameraChanged;
BetterToggle.ToggleValueChanged -= this.BetterToggle_ToggleValueChanged;
CharacterInstantiation.CharacterInstantiated -= this.CharacterInstantiation_CharacterInstantiated;
this.punVoiceNetwork.Client.StateChanged -= this.VoiceClientStateChanged;
PhotonNetwork.NetworkingClient.StateChanged -= this.PunClientStateChanged;
}
private void CharacterInstantiation_CharacterInstantiated(GameObject character)
{
if (this.recorder) // probably using a global recorder
{
return;
}
PhotonVoiceView photonVoiceView = character.GetComponent();
if (photonVoiceView.IsRecorder)
{
this.recorder = photonVoiceView.RecorderInUse;
}
}
private void InitToggles(Toggle[] toggles)
{
if (toggles == null) { return; }
for (int i = 0; i < toggles.Length; i++)
{
Toggle toggle = toggles[i];
switch (toggle.name)
{
case "Mute":
toggle.isOn = AudioListener.volume <= 0.001f;
break;
case "VoiceDetection":
toggle.isOn = this.recorder != null && this.recorder.VoiceDetection;
break;
case "DebugVoice":
toggle.isOn = this.DebugMode;
break;
case "Transmit":
toggle.isOn = this.recorder != null && this.recorder.TransmitEnabled;
break;
case "DebugEcho":
toggle.isOn = this.recorder != null && this.recorder.DebugEchoMode;
break;
case "AutoConnectAndJoin":
toggle.isOn = this.punVoiceNetwork.AutoConnectAndJoin;
break;
case "AutoLeaveAndDisconnect":
toggle.isOn = this.punVoiceNetwork.AutoLeaveAndDisconnect;
break;
}
}
}
private void BetterToggle_ToggleValueChanged(Toggle toggle)
{
switch (toggle.name)
{
case "Mute":
//AudioListener.pause = toggle.isOn;
if (toggle.isOn)
{
this.volumeBeforeMute = AudioListener.volume;
AudioListener.volume = 0f;
}
else
{
AudioListener.volume = this.volumeBeforeMute;
this.volumeBeforeMute = 0f;
}
break;
case "Transmit":
if (this.recorder)
{
this.recorder.TransmitEnabled = toggle.isOn;
}
break;
case "VoiceDetection":
if (this.recorder)
{
this.recorder.VoiceDetection = toggle.isOn;
}
break;
case "DebugEcho":
if (this.recorder)
{
this.recorder.DebugEchoMode = toggle.isOn;
}
break;
case "DebugVoice":
this.DebugMode = toggle.isOn;
break;
case "AutoConnectAndJoin":
this.punVoiceNetwork.AutoConnectAndJoin = toggle.isOn;
break;
case "AutoLeaveAndDisconnect":
this.punVoiceNetwork.AutoLeaveAndDisconnect = toggle.isOn;
break;
}
}
private void OnCameraChanged(Camera newCamera)
{
this.canvas.worldCamera = newCamera;
}
private void Start()
{
this.canvas = this.GetComponentInChildren