// -------------------------------------------------------------------------------------------------------------------- // // Part of: Photon Voice Utilities for Unity - Copyright (C) 2018 Exit Games GmbH // // // Simple component to call voiceConnection.ConnectUsingSettings() and get into a Voice room easily. // // // Requires a VoiceConnection component attached to the same GameObject. // // developer@exitgames.com // -------------------------------------------------------------------------------------------------------------------- using Photon.Realtime; using UnityEngine; using System.Collections.Generic; namespace Photon.Voice.Unity.UtilityScripts { [RequireComponent(typeof(VoiceConnection))] public class ConnectAndJoin : MonoBehaviour, IConnectionCallbacks, IMatchmakingCallbacks { private VoiceConnection voiceConnection; public bool RandomRoom = true; [SerializeField] private bool autoConnect = true; [SerializeField] private bool autoTransmit = true; [SerializeField] private bool publishUserId = false; public string RoomName; private readonly EnterRoomParams enterRoomParams = new EnterRoomParams { RoomOptions = new RoomOptions() }; public bool IsConnected { get { return this.voiceConnection.Client.IsConnected; } } private void Awake() { this.voiceConnection = this.GetComponent(); } private void OnEnable() { this.voiceConnection.Client.AddCallbackTarget(this); if (this.autoConnect) { this.ConnectNow(); } } private void OnDisable() { this.voiceConnection.Client.RemoveCallbackTarget(this); } public void ConnectNow() { this.voiceConnection.ConnectUsingSettings(); } #region MatchmakingCallbacks public void OnCreatedRoom() { } public void OnCreateRoomFailed(short returnCode, string message) { Debug.LogErrorFormat("OnCreateRoomFailed errorCode={0} errorMessage={1}", returnCode, message); } public void OnFriendListUpdate(List friendList) { } public void OnJoinedRoom() { if (this.voiceConnection.PrimaryRecorder == null) { this.voiceConnection.PrimaryRecorder = this.gameObject.AddComponent(); } if (this.autoTransmit) { this.voiceConnection.PrimaryRecorder.TransmitEnabled = this.autoTransmit; } } public void OnJoinRandomFailed(short returnCode, string message) { Debug.LogErrorFormat("OnJoinRandomFailed errorCode={0} errorMessage={1}", returnCode, message); } public void OnJoinRoomFailed(short returnCode, string message) { Debug.LogErrorFormat("OnJoinRoomFailed roomName={0} errorCode={1} errorMessage={2}", this.RoomName, returnCode, message); } public void OnLeftRoom() { } #endregion #region ConnectionCallbacks public void OnConnected() { } public void OnConnectedToMaster() { this.enterRoomParams.RoomOptions.PublishUserId = this.publishUserId; if (this.RandomRoom) { this.enterRoomParams.RoomName = null; this.voiceConnection.Client.OpJoinRandomOrCreateRoom(new OpJoinRandomRoomParams(), this.enterRoomParams); } else { this.enterRoomParams.RoomName = this.RoomName; this.voiceConnection.Client.OpJoinOrCreateRoom(this.enterRoomParams); } } public void OnDisconnected(DisconnectCause cause) { if (cause == DisconnectCause.None || cause == DisconnectCause.DisconnectByClientLogic) { return; } Debug.LogErrorFormat("OnDisconnected cause={0}", cause); } public void OnRegionListReceived(RegionHandler regionHandler) { } public void OnCustomAuthenticationResponse(Dictionary data) { } public void OnCustomAuthenticationFailed(string debugMessage) { } #endregion } }