Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Photon.Pun;
- using Photon.Realtime;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- public class NetworkManager : MonoBehaviourPunCallbacks
- {
- public List<string> maps = new List<string>();
- [SerializeField] private GameObject menuParent;
- [SerializeField] private GameObject preMatchLobbyParent;
- [SerializeField] private TMP_Text timerText;
- [SerializeField] private int minPlayers;
- string gameMode;
- float timer;
- bool timerOn;
- [HideInInspector] public bool connected;
- [HideInInspector] public bool backToMenu;
- void Awake()
- {
- PhotonNetwork.AutomaticallySyncScene = true;
- }
- void Start()
- {
- if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
- {
- connected = false;
- menuParent.SetActive(true);
- preMatchLobbyParent.SetActive(false);
- timer = 6;
- timerText.gameObject.SetActive(false);
- }
- }
- void Update()
- {
- if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
- {
- if(preMatchLobbyParent.activeSelf)
- {
- if (PhotonNetwork.PlayerList.Length >= minPlayers && connected)
- {
- timerOn = true;
- }
- else
- {
- timerOn = false;
- timer = 6;
- }
- if (timerOn)
- {
- timerText.gameObject.SetActive(true);
- timer -= Time.deltaTime;
- if (timer <= 0)
- {
- string sceneToLoad = maps[Random.Range(0, maps.Count)];
- PhotonNetwork.LoadLevel(sceneToLoad);
- }
- }
- timerText.text = timer.ToString("0");
- }
- }
- }
- public void Connect(string whichGameMode)
- {
- gameMode = whichGameMode;
- timer = 6;
- menuParent.SetActive(false);
- preMatchLobbyParent.SetActive(true);
- PhotonNetwork.ConnectUsingSettings();
- }
- public void Disconnect()
- {
- connected = false;
- StartCoroutine(WaitUntilDisconnect());
- }
- public override void OnConnectedToMaster()
- {
- PhotonNetwork.JoinLobby();
- }
- public override void OnJoinedLobby()
- {
- //ExitGames.Client.Photon.Hashtable expectedRoomProperties = new ExitGames.Client.Photon.Hashtable() { { gameMode, 1 } };
- //PhotonNetwork.JoinRandomRoom(expectedRoomProperties, 5);
- PhotonNetwork.JoinRandomRoom();
- }
- public override void OnJoinRandomFailed(short returnCode, string message)
- {
- //string[] roomProperties = { gameMode };
- //RoomOptions roomOptions = new RoomOptions();
- //roomOptions.CustomRoomPropertiesForLobby = roomProperties;
- //roomOptions.MaxPlayers = 5;
- //PhotonNetwork.CreateRoom(null, roomOptions, null);
- PhotonNetwork.CreateRoom(null);
- }
- public override void OnJoinedRoom()
- {
- connected = true;
- }
- public override void OnDisconnected(DisconnectCause cause)
- {
- if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
- {
- preMatchLobbyParent.SetActive(false);
- menuParent.SetActive(true);
- backToMenu = true;
- }
- }
- IEnumerator WaitUntilDisconnect()
- {
- yield return new WaitForSeconds(0.1f);
- PhotonNetwork.Disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement