2017-09-21 04:59:13 -05:00
using UnityEngine ;
using UnityEngine.UI ;
namespace Com.MyCompany.MyGame
{
public class Launcher : Photon . PunBehaviour
{
2017-09-28 01:17:01 -05:00
public byte MaxPlayersPerRoom = 4 ;
2017-09-21 04:59:13 -05:00
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
public GameObject controlPanel ;
2017-09-28 01:17:01 -05:00
2017-09-21 04:59:13 -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
public GameObject nameSelectorPanel , roomSelectorPanel ;
public GameObject availableRoomsPanel ;
public GameObject roomStatusPanelPrefab ;
2017-09-21 04:59:13 -05:00
string _gameVersion = "1" ;
/// <summary>
/// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon,
/// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
/// Typically this is used for the OnConnectedToMaster() callback.
/// </summary>
bool isConnecting ;
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 ;
}
void Start ( )
{
controlPanel . SetActive ( true ) ;
}
public void Connect ( )
{
isConnecting = true ;
progressLabel . text = "Connecting..." ;
// we check if we are connected or not, we join if we are , else we initiate the connection to the server.
if ( PhotonNetwork . connected )
{
2017-09-28 01:17:01 -05:00
PhotonNetwork . JoinLobby ( ) ;
2017-09-21 04:59:13 -05:00
}
else
{
PhotonNetwork . ConnectUsingSettings ( _gameVersion ) ;
}
}
public override void OnConnectedToMaster ( )
{
2017-09-28 01:17:01 -05:00
Debug . Log ( "Launcher: OnConnectedToMaster() was called by PUN" ) ;
2017-09-21 04:59:13 -05:00
// we don't want to do anything if we are not attempting to join a room.
// this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
// we don't want to do anything.
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 OnPhotonRandomJoinFailed()
2017-09-28 01:17:01 -05:00
PhotonNetwork . JoinLobby ( ) ;
//PhotonNetwork.JoinRandomRoom();
2017-09-21 04:59:13 -05:00
}
}
2017-09-28 01:17:01 -05:00
public override void OnJoinedLobby ( )
{
Debug . Log ( "Launcher: Entered Lobby" ) ;
nameSelectorPanel . SetActive ( false ) ;
roomSelectorPanel . SetActive ( true ) ;
RefreshRoomsList ( ) ;
}
public void RefreshRoomsList ( )
{
RoomInfo [ ] roomsList = PhotonNetwork . GetRoomList ( ) ;
Debug . Log ( "Number of rooms available: " + roomsList . Length ) ;
foreach ( Transform child in availableRoomsPanel . transform )
{
Destroy ( child . gameObject ) ;
}
foreach ( RoomInfo roomInfo in roomsList )
{
var roomStatusPanel = Instantiate ( roomStatusPanelPrefab , availableRoomsPanel . transform ) ;
roomStatusPanel . GetComponent < RoomStatusPanel > ( ) . SetInformation ( roomInfo ) ;
}
}
public void CreateNewRoom ( )
{
PhotonNetwork . CreateRoom ( null , new RoomOptions ( ) { MaxPlayers = MaxPlayersPerRoom } , null ) ;
}
2017-09-21 04:59:13 -05:00
public override void OnDisconnectedFromPhoton ( )
{
2017-09-28 01:17:01 -05:00
Debug . LogWarning ( "Launcher: OnDisconnectedFromPhoton() was called by PUN" ) ;
2017-09-21 04:59:13 -05:00
}
2017-09-28 01:17:01 -05:00
/ *
2017-09-21 04:59:13 -05:00
public override void OnPhotonRandomJoinFailed ( object [ ] codeAndMsg )
{
2017-09-28 01:17:01 -05:00
Debug . Log ( "Launcher:OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one." ) ;
2017-09-21 04:59:13 -05:00
// #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
PhotonNetwork . CreateRoom ( null , new RoomOptions ( ) { MaxPlayers = MaxPlayersPerRoom } , null ) ;
2017-09-28 01:17:01 -05:00
} * /
2017-09-21 04:59:13 -05:00
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" ) ;
}
}
}