2017-09-21 04:59:13 -05:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public class Launcher : Photon.PunBehaviour
|
|
|
|
|
{
|
|
|
|
|
public byte MaxPlayersPerRoom = 4;
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
|
|
|
|
|
public GameObject controlPanel;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
[Tooltip("The UI Label to inform the user that the connection is in progress")]
|
|
|
|
|
public Text progressLabel;
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public GameObject nameSelectorPanel, roomSelectorPanel;
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public GameObject availableRoomsPanel;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public GameObject roomStatusPanelPrefab;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
string _gameVersion = "1";
|
|
|
|
|
bool isConnecting;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
// #Critical
|
|
|
|
|
// we don't join the lobby. There is no need to join a lobby to get the list of rooms.
|
|
|
|
|
PhotonNetwork.autoJoinLobby = false;
|
|
|
|
|
|
|
|
|
|
// #Critical
|
|
|
|
|
// This makes sure we can use PhotonNetwork.LoadLevel() on the master client
|
|
|
|
|
// and all clients in the same room sync their level automatically
|
|
|
|
|
PhotonNetwork.automaticallySyncScene = true;
|
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
controlPanel.SetActive(true);
|
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public void Connect()
|
|
|
|
|
{
|
|
|
|
|
isConnecting = true;
|
|
|
|
|
progressLabel.text = "Connecting...";
|
|
|
|
|
if (PhotonNetwork.connected)
|
2017-09-21 04:59:13 -05:00
|
|
|
|
{
|
2017-09-28 01:42:24 -05:00
|
|
|
|
PhotonNetwork.JoinLobby();
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
else
|
2017-09-21 04:59:13 -05:00
|
|
|
|
{
|
2017-09-28 01:42:24 -05:00
|
|
|
|
PhotonNetwork.ConnectUsingSettings(_gameVersion);
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public override void OnConnectedToMaster()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Launcher: OnConnectedToMaster() was called by PUN");
|
|
|
|
|
if (isConnecting)
|
2017-09-21 04:59:13 -05:00
|
|
|
|
{
|
2017-09-28 01:42:24 -05:00
|
|
|
|
PhotonNetwork.JoinLobby();
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public override void OnJoinedLobby()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Launcher: Entered Lobby");
|
|
|
|
|
nameSelectorPanel.SetActive(false);
|
|
|
|
|
roomSelectorPanel.SetActive(true);
|
|
|
|
|
RefreshRoomsList();
|
|
|
|
|
}
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public override void OnPhotonJoinRoomFailed(object[] codeAndMsg)
|
|
|
|
|
{
|
|
|
|
|
RefreshRoomsList();
|
|
|
|
|
}
|
2017-09-28 01:17:01 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public override void OnReceivedRoomListUpdate()
|
|
|
|
|
{
|
|
|
|
|
RefreshRoomsList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RefreshRoomsList()
|
|
|
|
|
{
|
|
|
|
|
RoomInfo[] roomsList = PhotonNetwork.GetRoomList();
|
|
|
|
|
foreach (Transform child in availableRoomsPanel.transform)
|
2017-09-28 01:17:01 -05:00
|
|
|
|
{
|
2017-09-28 01:42:24 -05:00
|
|
|
|
Destroy(child.gameObject);
|
2017-09-28 01:17:01 -05:00
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
foreach (RoomInfo roomInfo in roomsList)
|
2017-09-21 04:59:13 -05:00
|
|
|
|
{
|
2017-09-28 01:42:24 -05:00
|
|
|
|
var panel = Instantiate(roomStatusPanelPrefab, availableRoomsPanel.transform);
|
|
|
|
|
var roomStatusPanel = panel.GetComponent<RoomStatusPanel>();
|
|
|
|
|
roomStatusPanel.SetInformation(roomInfo);
|
|
|
|
|
roomStatusPanel.launcher = this;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public void RoomSelected(RoomStatusPanel panel)
|
|
|
|
|
{
|
|
|
|
|
PhotonNetwork.JoinRoom(panel.GetRoomName());
|
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-28 01:42:24 -05:00
|
|
|
|
public void CreateNewRoom()
|
|
|
|
|
{
|
|
|
|
|
string roomName = PhotonNetwork.playerName + "'s Room";
|
|
|
|
|
PhotonNetwork.CreateRoom(roomName, new RoomOptions() { MaxPlayers = MaxPlayersPerRoom }, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnDisconnectedFromPhoton()
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Launcher: OnDisconnectedFromPhoton() was called by PUN");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnJoinedRoom()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
|
|
|
|
|
// #Critical: We only load if we are the first player, else we rely on PhotonNetwork.automaticallySyncScene to sync our instance scene.
|
|
|
|
|
PhotonNetwork.LoadLevel("Lobby");
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-28 01:42:24 -05:00
|
|
|
|
|