Search Unity

Third Party Questions Regarding PUN Code

Discussion in 'Multiplayer' started by jettzeong, Aug 27, 2021.

  1. jettzeong

    jettzeong

    Joined:
    Nov 5, 2018
    Posts:
    65
    Code (CSharp):
    1.  
    2. using Photon.Pun;
    3. using Photon.Realtime;
    4. using TMPro;
    5. using UnityEngine;
    6.  
    7. namespace PhotonTutorial.Menus
    8. {
    9.     public class MainMenu : MonoBehaviourPunCallbacks
    10.     {
    11.         [SerializeField] private GameObject findOpponentPanel = null;
    12.         [SerializeField] private GameObject waitingStatusPanel = null;
    13.         [SerializeField] private TextMeshProUGUI waitingStatusText = null;
    14.  
    15.         private bool isConnecting = false;
    16.  
    17.         private const string GameVersion = "0.1";
    18.         private const int MaxPlayersPerRoom = 2;
    19.  
    20.         private void Awake() => PhotonNetwork.AutomaticallySyncScene = true;
    21.  
    22.         public void FindOpponent()
    23.         {
    24.             isConnecting = true;
    25.  
    26.             findOpponentPanel.SetActive(false);
    27.             waitingStatusPanel.SetActive(true);
    28.  
    29.             waitingStatusText.text = "Searching...";
    30.  
    31.             if (PhotonNetwork.IsConnected)
    32.             {
    33.                 PhotonNetwork.JoinRandomRoom();
    34.             }
    35.             else
    36.             {
    37.                 PhotonNetwork.GameVersion = GameVersion;
    38.                 PhotonNetwork.ConnectUsingSettings();
    39.             }
    40.         }
    41.  
    42.         public override void OnConnectedToMaster()
    43.         {
    44.             Debug.Log("Connected To Master");
    45.  
    46.             if (isConnecting)
    47.             {
    48.                 PhotonNetwork.JoinRandomRoom();
    49.             }
    50.         }
    51.  
    52.         public override void OnDisconnected(DisconnectCause cause)
    53.         {
    54.             waitingStatusPanel.SetActive(false);
    55.             findOpponentPanel.SetActive(true);
    56.  
    57.             Debug.Log($"Disconnected due to: {cause}");
    58.         }
    59.  
    60.         public override void OnJoinRandomFailed(short returnCode, string message)
    61.         {
    62.             Debug.Log("No clients are waiting for an opponent, creating a new room");
    63.  
    64.             PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = MaxPlayersPerRoom });
    65.         }
    66.  
    67.         public override void OnJoinedRoom()
    68.         {
    69.             Debug.Log("Client successfully joined a room");
    70.  
    71.             int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
    72.  
    73.             if(playerCount != MaxPlayersPerRoom)
    74.             {
    75.                 waitingStatusText.text = "Waiting For Opponent";
    76.                 Debug.Log("Client is waiting for an opponent");
    77.             }
    78.             else
    79.             {
    80.                 waitingStatusText.text = "Opponent Found";
    81.                 Debug.Log("Match is ready to begin");
    82.             }
    83.         }
    84.  
    85.         public override void OnPlayerEnteredRoom(Player newPlayer)
    86.         {
    87.             if(PhotonNetwork.CurrentRoom.PlayerCount == MaxPlayersPerRoom)
    88.             {
    89.                 PhotonNetwork.CurrentRoom.IsOpen  = false;
    90.  
    91.                 waitingStatusText.text = "Opponent Found";
    92.                 Debug.Log("Match is ready to begin");
    93.  
    94.                 PhotonNetwork.LoadLevel("Scene_Main");
    95.             }
    96.         }
    97.     }
    98.  
    I am confused by what the code below actually does. Can anyone explain why this piece of code is required in simple terms?

    Code (CSharp):
    1.     if (PhotonNetwork.IsConnected)
    2.             {
    3.                 PhotonNetwork.JoinRandomRoom();
    4.             }
    5.             else
    6.             {
    7.                 PhotonNetwork.GameVersion = GameVersion;
    8.                 PhotonNetwork.ConnectUsingSettings();
    9.             }