// --------------------------------------------------------------------------------------------------------------------
//
// Part of: Photon Unity Networking Demos
//
//
// Used in "PUN Basic tutorial" to handle typical game management requirements
//
// developer@exitgames.com
// --------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using UnityEngine.SceneManagement;
using Photon.Realtime;
namespace Photon.Pun.Demo.PunBasics
{
#pragma warning disable 649
///
/// Game manager.
/// Connects and watch Photon Status, Instantiate Player
/// Deals with quiting the room and the game
/// Deals with level loading (outside the in room synchronization)
///
public class GameManager : MonoBehaviourPunCallbacks
{
#region Public Fields
static public GameManager Instance;
#endregion
#region Private Fields
private GameObject instance;
[Tooltip("The prefab to use for representing the player")]
[SerializeField]
private GameObject playerPrefab;
#endregion
#region MonoBehaviour CallBacks
///
/// MonoBehaviour method called on GameObject by Unity during initialization phase.
///
void Start()
{
Instance = this;
// in case we started this demo with the wrong scene being active, simply load the menu scene
if (!PhotonNetwork.IsConnected)
{
SceneManager.LoadScene("PunBasics-Launcher");
return;
}
if (playerPrefab == null) { // #Tip Never assume public properties of Components are filled up properly, always check and inform the developer of it.
Debug.LogError("Missing playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
} else {
if (PlayerManager.LocalPlayerInstance==null)
{
Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f,5f,0f), Quaternion.identity, 0);
}else{
Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
}
}
}
///
/// MonoBehaviour method called on GameObject by Unity on every frame.
///
void Update()
{
// "back" button of phone equals "Escape". quit app if that's pressed
if (Input.GetKeyDown(KeyCode.Escape))
{
QuitApplication();
}
}
#endregion
#region Photon Callbacks
///
/// Called when a Photon Player got connected. We need to then load a bigger scene.
///
/// Other.
public override void OnPlayerEnteredRoom( Player other )
{
Debug.Log( "OnPlayerEnteredRoom() " + other.NickName); // not seen if you're the player connecting
if ( PhotonNetwork.IsMasterClient )
{
Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
LoadArena();
}
}
///
/// Called when a Photon Player got disconnected. We need to load a smaller scene.
///
/// Other.
public override void OnPlayerLeftRoom( Player other )
{
Debug.Log( "OnPlayerLeftRoom() " + other.NickName ); // seen when other disconnects
if ( PhotonNetwork.IsMasterClient )
{
Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
LoadArena();
}
}
///
/// Called when the local player left the room. We need to load the launcher scene.
///
public override void OnLeftRoom()
{
SceneManager.LoadScene("PunBasics-Launcher");
}
#endregion
#region Public Methods
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
public void QuitApplication()
{
Application.Quit();
}
#endregion
#region Private Methods
void LoadArena()
{
if ( ! PhotonNetwork.IsMasterClient )
{
Debug.LogError( "PhotonNetwork : Trying to Load a level but we are not the master Client" );
}
Debug.LogFormat( "PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount );
PhotonNetwork.LoadLevel("PunBasics-Room for "+PhotonNetwork.CurrentRoom.PlayerCount);
}
#endregion
}
}