Advertisement
Guest User

Untitled

a guest
Mar 19th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using Photon.Pun;
  4. using Photon.Realtime;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using TMPro;
  8.  
  9. public class NetworkManager : MonoBehaviourPunCallbacks
  10. {
  11.     public List<string> maps = new List<string>();
  12.     [SerializeField] private GameObject menuParent;
  13.     [SerializeField] private GameObject preMatchLobbyParent;
  14.     [SerializeField] private TMP_Text timerText;
  15.     [SerializeField] private int minPlayers;
  16.     string gameMode;    
  17.     float timer;
  18.     bool timerOn;
  19.     [HideInInspector] public bool connected;
  20.     [HideInInspector] public bool backToMenu;
  21.  
  22.     void Awake()
  23.     {
  24.         PhotonNetwork.AutomaticallySyncScene = true;
  25.     }
  26.  
  27.     void Start()
  28.     {
  29.         if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
  30.         {
  31.             connected = false;
  32.             menuParent.SetActive(true);
  33.             preMatchLobbyParent.SetActive(false);
  34.             timer = 6;
  35.             timerText.gameObject.SetActive(false);
  36.         }  
  37.     }
  38.  
  39.     void Update()
  40.     {
  41.         if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
  42.         {
  43.             if(preMatchLobbyParent.activeSelf)
  44.             {
  45.                 if (PhotonNetwork.PlayerList.Length >= minPlayers && connected)
  46.                 {
  47.                     timerOn = true;
  48.                 }
  49.                 else
  50.                 {
  51.                     timerOn = false;
  52.                     timer = 6;
  53.                 }
  54.                 if (timerOn)
  55.                 {
  56.                     timerText.gameObject.SetActive(true);
  57.                     timer -= Time.deltaTime;
  58.                     if (timer <= 0)
  59.                     {
  60.                         string sceneToLoad = maps[Random.Range(0, maps.Count)];
  61.                         PhotonNetwork.LoadLevel(sceneToLoad);
  62.                     }
  63.                 }
  64.                 timerText.text = timer.ToString("0");
  65.             }        
  66.         }      
  67.     }
  68.  
  69.     public void Connect(string whichGameMode)
  70.     {      
  71.         gameMode = whichGameMode;
  72.         timer = 6;
  73.         menuParent.SetActive(false);
  74.         preMatchLobbyParent.SetActive(true);
  75.         PhotonNetwork.ConnectUsingSettings();
  76.     }
  77.  
  78.     public void Disconnect()
  79.     {
  80.         connected = false;
  81.         StartCoroutine(WaitUntilDisconnect());
  82.     }
  83.  
  84.     public override void OnConnectedToMaster()
  85.     {
  86.         PhotonNetwork.JoinLobby();
  87.     }
  88.  
  89.     public override void OnJoinedLobby()
  90.     {
  91.         //ExitGames.Client.Photon.Hashtable expectedRoomProperties = new ExitGames.Client.Photon.Hashtable() { { gameMode, 1 } };
  92.         //PhotonNetwork.JoinRandomRoom(expectedRoomProperties, 5);
  93.         PhotonNetwork.JoinRandomRoom();
  94.     }
  95.  
  96.     public override void OnJoinRandomFailed(short returnCode, string message)
  97.     {
  98.         //string[] roomProperties = { gameMode };
  99.         //RoomOptions roomOptions = new RoomOptions();
  100.         //roomOptions.CustomRoomPropertiesForLobby = roomProperties;
  101.         //roomOptions.MaxPlayers = 5;
  102.         //PhotonNetwork.CreateRoom(null, roomOptions, null);
  103.         PhotonNetwork.CreateRoom(null);
  104.     }
  105.    
  106.     public override void OnJoinedRoom()
  107.     {
  108.         connected = true;
  109.     }
  110.  
  111.     public override void OnDisconnected(DisconnectCause cause)
  112.     {
  113.         if(SceneManager.GetActiveScene().name == "MultiplayerMenu")
  114.         {
  115.             preMatchLobbyParent.SetActive(false);
  116.             menuParent.SetActive(true);
  117.             backToMenu = true;          
  118.         }      
  119.     }
  120.  
  121.     IEnumerator WaitUntilDisconnect()
  122.     {
  123.         yield return new WaitForSeconds(0.1f);
  124.         PhotonNetwork.Disconnect();
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement