Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Third Party PUN2 - Advice on Improving Readability of Code

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

  1. jettzeong

    jettzeong

    Joined:
    Nov 5, 2018
    Posts:
    65
    Hi all, I am new to PUN2 and was just trying to create a system where there can be a join room and create room function. The picture below explains how it works:

    upload_2021-8-29_21-53-18.png
    1. Players connect to photon server by clicking connect (purely for experimental purposes)

    upload_2021-8-29_21-54-13.png

    2. Players reach the main panel which allows them to create or join room

    upload_2021-8-29_21-54-55.png

    3. If they create a room, they will have to type in the id for new room

    upload_2021-8-29_21-55-35.png
    4. If they want to join a room, they will have to type in the room id that they want to join

    upload_2021-8-29_22-0-25.png
    5. After connecting, there will be a waiting status text, which is disabled once the max amount of players are in the room. Then, the players will be loaded into the level.

    This is my first time doing this, so I am quite worried on potential errors that may come out if the code is written like this. The panel.setActive bits also feels a bit redundant, and I would like to change that to a cleaner implementation if possible. Or if possible, do share any good practices for using PUN in general! I would really like to know and improve :)

    Below is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.Pun;
    6. using Photon.Realtime;
    7.  
    8. namespace Testing
    9.  
    10. {
    11.     public class MenuManager : MonoBehaviourPunCallbacks
    12.     {
    13.         [SerializeField] private Button createRoomButton;
    14.         [SerializeField] private Button joinRoomButton;
    15.         [SerializeField] private Button connectButton;
    16.         [SerializeField] private const int maxPlayersPerRoom = 2;
    17.         public Text connectingStatusText;
    18.         public Text waitingStatusText;
    19.         private bool isConnected = false;
    20.         public GameObject panel_Main;
    21.         public GameObject panel_CreateRoom;
    22.         public GameObject panel_JoinRoom;
    23.         public GameObject panel_ConnectToPhoton;
    24.         public GameObject panel_Waiting;
    25.         public InputField newRoomId;
    26.         public InputField joiningRoomId;
    27.  
    28.         private void Awake()
    29.         {
    30.             PhotonNetwork.AutomaticallySyncScene = true;
    31.         }
    32.         public void ConnectToPhoton()
    33.         {
    34.             if(isConnected == false)
    35.             {
    36.                 PhotonNetwork.ConnectUsingSettings();
    37.             }
    38.         }
    39.         public override void OnConnectedToMaster()
    40.         {
    41.             connectingStatusText.text = "Connected!";
    42.             Debug.Log("Connected To Master");
    43.             isConnected = true;
    44.             panel_ConnectToPhoton.SetActive(false);
    45.             panel_Main.SetActive(true);
    46.         }
    47.         public override void OnDisconnected (DisconnectCause cause)
    48.         {
    49.             Debug.Log($"Disconnected due to {cause}");
    50.             connectingStatusText.text = $"Not Connected. Disconnect due to {cause}";
    51.         }
    52.         public void CreateRoom() //does not actually create room
    53.         {
    54.             panel_Main.SetActive(false);
    55.             panel_CreateRoom.SetActive(true);
    56.         }
    57.         public void JoinRoom()
    58.         {
    59.             panel_Main.SetActive(false);
    60.             panel_JoinRoom.SetActive(true);
    61.         }
    62.         public void CreateNewRoom() //actually creates the room
    63.         {
    64.             Debug.Log("Created new room with ID" + newRoomId.text);
    65.             PhotonNetwork.JoinOrCreateRoom(newRoomId.text, new RoomOptions { MaxPlayers = maxPlayersPerRoom }, null);
    66.         }
    67.         public void JoinExistingRoom()
    68.         {
    69.             PhotonNetwork.JoinRoom(joiningRoomId.text);
    70.         }
    71.         public override void OnJoinedRoom()
    72.         {
    73.             Debug.Log("Client successfully joined a room");
    74.             panel_CreateRoom.SetActive(false);
    75.             panel_JoinRoom.SetActive(false);
    76.             panel_Waiting.SetActive(true);
    77.  
    78.             int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
    79.  
    80.             if (playerCount != maxPlayersPerRoom)
    81.             {
    82.                 waitingStatusText.text = "Waiting For Opponent";
    83.                 Debug.Log("Client is waiting for an opponent");
    84.             }
    85.  
    86.             else
    87.             {
    88.                 waitingStatusText.text = "Opponent Found!";
    89.                 Debug.Log("Match is ready to begin");
    90.             }
    91.         }
    92.         public override void OnJoinRoomFailed(short returnCode, string message)
    93.         {
    94.             Debug.Log("Failed to join room due to " + returnCode );
    95.         }
    96.         public override void OnPlayerEnteredRoom(Player newPlayer)
    97.         {
    98.             if (PhotonNetwork.CurrentRoom.PlayerCount == maxPlayersPerRoom)
    99.             {
    100.                 PhotonNetwork.CurrentRoom.IsOpen = false;
    101.                 Debug.Log("Match is ready to begin");
    102.  
    103.                 PhotonNetwork.LoadLevel("Scene_Main");
    104.  
    105.             }
    106.         }
    107.     }
    108. }
    109.  
    110.  
    111.