Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using Photon.Pun;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using Photon.Realtime;
  5.  
  6. public class Launch : MonoBehaviourPunCallbacks
  7. {
  8.  
  9. private GameObject controlPanel; // the panel that holds the UI
  10.  
  11. private Text feedbackText; // gives us information about the connection status
  12.  
  13. private byte maxPlayersPerRoom = 4; // max number of player to play the multiplayer session
  14.  
  15. bool isConnecting; // check if we are currently connecting
  16.  
  17. void Awake()
  18. {
  19. PhotonNetwork.AutomaticallySyncScene = true; // we dont wanna sync everything auto
  20. }
  21.  
  22. // Method for connecting to the server
  23. public void Connect()
  24. {
  25. feedbackText.text = "";
  26.  
  27. isConnecting = true;
  28.  
  29. controlPanel.SetActive(false);
  30.  
  31. if (PhotonNetwork.IsConnected) // check if we are connected to network
  32. {
  33. LogFeedback("Joining Room...");// change the feedback text
  34. PhotonNetwork.JoinRandomRoom(); // if we are connected then join a random room
  35. }
  36. else
  37. {
  38.  
  39. LogFeedback("Connecting..."); //change the feedback text
  40.  
  41. PhotonNetwork.ConnectUsingSettings(); // if we are not connected yet, then try to connect
  42. }
  43. }
  44.  
  45.  
  46. void LogFeedback(string message)
  47. {
  48. if (feedbackText == null) {
  49. return;
  50. }
  51.  
  52. feedbackText.text += System.Environment.NewLine+message;
  53. }
  54.  
  55. public override void OnConnectedToMaster()
  56. {
  57.  
  58. if (isConnecting)
  59. {
  60. PhotonNetwork.JoinRandomRoom();
  61. }
  62. }
  63.  
  64. public override void OnJoinRandomFailed(short returnCode, string message)
  65. {
  66. PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = this.maxPlayersPerRoom});
  67. }
  68.  
  69.  
  70.  
  71. public override void OnDisconnected(DisconnectCause cause)
  72. {
  73. isConnecting = false;
  74. controlPanel.SetActive(true);
  75.  
  76. }
  77.  
  78. public override void OnJoinedRoom()
  79. {
  80. if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
  81. {
  82. PhotonNetwork.LoadLevel("PunBasics-Room for 1");
  83.  
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement