105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
|
using Photon.Pun;
|
||
|
using Photon.Realtime;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace cc.ivanli.pun01
|
||
|
{
|
||
|
public class Launcher : MonoBehaviourPunCallbacks
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Client's version
|
||
|
/// </summary>
|
||
|
private readonly string gameVersion = "1";
|
||
|
|
||
|
[Tooltip("The maximum number of players per room. When a room is full, it can't be joined by new players, and so new room will be created")]
|
||
|
[SerializeField]
|
||
|
private byte maxPlayersPerRoom = 4;
|
||
|
|
||
|
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
|
||
|
[SerializeField]
|
||
|
private GameObject controlPanel;
|
||
|
|
||
|
[Tooltip("The UI Label to inform the user that the connection is in progress")]
|
||
|
[SerializeField]
|
||
|
private GameObject progressLabel;
|
||
|
|
||
|
bool isConnecting;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
PhotonNetwork.AutomaticallySyncScene = true;
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
progressLabel.SetActive(false);
|
||
|
controlPanel.SetActive(true);
|
||
|
}
|
||
|
|
||
|
public void Connect()
|
||
|
{
|
||
|
progressLabel.SetActive(true);
|
||
|
controlPanel.SetActive(false);
|
||
|
if (PhotonNetwork.IsConnected)
|
||
|
{
|
||
|
PhotonNetwork.JoinRandomRoom();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
isConnecting = PhotonNetwork.ConnectUsingSettings();
|
||
|
PhotonNetwork.GameVersion = gameVersion;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override void OnConnectedToMaster()
|
||
|
{
|
||
|
Debug.Log("PUN Basics Launcher:OnConnectedToMaster() was called by PUN");
|
||
|
base.OnConnectedToMaster();
|
||
|
if (isConnecting)
|
||
|
{
|
||
|
// #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
|
||
|
PhotonNetwork.JoinRandomRoom();
|
||
|
isConnecting = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override void OnDisconnected(DisconnectCause cause)
|
||
|
{
|
||
|
progressLabel.SetActive(false);
|
||
|
controlPanel.SetActive(true);
|
||
|
isConnecting = false;
|
||
|
|
||
|
Debug.LogWarningFormat("PUN basics Launcher:OnDisconnected() was called by PUN with reason {0}", cause);
|
||
|
base.OnDisconnected(cause);
|
||
|
}
|
||
|
|
||
|
public override void OnJoinRandomFailed(short returnCode, string message)
|
||
|
{
|
||
|
Debug.Log("PUN BasicsLauncher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom()");
|
||
|
base.OnJoinRandomFailed(returnCode, message);
|
||
|
|
||
|
PhotonNetwork.CreateRoom(null, new RoomOptions
|
||
|
{
|
||
|
MaxPlayers = maxPlayersPerRoom
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public override void OnJoinedRoom()
|
||
|
{
|
||
|
Debug.Log("PUN Basics Launcher:OnJoinedRoom() called by PUN. Now this client is in a room.");
|
||
|
base.OnJoinedRoom();
|
||
|
|
||
|
// #Critical: We only load if we are the first player, else we rely on `PhotonNetwork.AutomaticallySyncScene` to sync our instance scene.
|
||
|
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
|
||
|
{
|
||
|
Debug.Log("We load the 'Room for 1' ");
|
||
|
|
||
|
|
||
|
// #Critical
|
||
|
// Load the Room Level.
|
||
|
PhotonNetwork.LoadLevel("Room for 1");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|