Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Photon.Pun;
- using UnityEngine;
- using UnityEngine.UI;
- using Photon.Realtime;
- public class Launch : MonoBehaviourPunCallbacks
- {
- private GameObject controlPanel; // the panel that holds the UI
- private Text feedbackText; // gives us information about the connection status
- private byte maxPlayersPerRoom = 4; // max number of player to play the multiplayer session
- bool isConnecting; // check if we are currently connecting
- void Awake()
- {
- PhotonNetwork.AutomaticallySyncScene = true; // we dont wanna sync everything auto
- }
- // Method for connecting to the server
- public void Connect()
- {
- feedbackText.text = "";
- isConnecting = true;
- controlPanel.SetActive(false);
- if (PhotonNetwork.IsConnected) // check if we are connected to network
- {
- LogFeedback("Joining Room...");// change the feedback text
- PhotonNetwork.JoinRandomRoom(); // if we are connected then join a random room
- }
- else
- {
- LogFeedback("Connecting..."); //change the feedback text
- PhotonNetwork.ConnectUsingSettings(); // if we are not connected yet, then try to connect
- }
- }
- void LogFeedback(string message)
- {
- if (feedbackText == null) {
- return;
- }
- feedbackText.text += System.Environment.NewLine+message;
- }
- public override void OnConnectedToMaster()
- {
- if (isConnecting)
- {
- PhotonNetwork.JoinRandomRoom();
- }
- }
- public override void OnJoinRandomFailed(short returnCode, string message)
- {
- PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = this.maxPlayersPerRoom});
- }
- public override void OnDisconnected(DisconnectCause cause)
- {
- isConnecting = false;
- controlPanel.SetActive(true);
- }
- public override void OnJoinedRoom()
- {
- if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
- {
- PhotonNetwork.LoadLevel("PunBasics-Room for 1");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement