123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- // --------------------------------------------------------------------------------------------------------------------
- // <copyright file="Launcher.cs" company="Exit Games GmbH">
- // Part of: Photon Unity Networking Demos
- // </copyright>
- // <summary>
- // Used in "PUN Basic tutorial" to handle typical game management requirements
- // </summary>
- // <author>developer@exitgames.com</author>
- // --------------------------------------------------------------------------------------------------------------------
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Photon.Realtime;
- namespace Photon.Pun.Demo.PunBasics
- {
- #pragma warning disable 649
- /// <summary>
- /// 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)
- /// </summary>
- 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
- /// <summary>
- /// MonoBehaviour method called on GameObject by Unity during initialization phase.
- /// </summary>
- 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("<Color=Red><b>Missing</b></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
- } else {
- if (PhotonNetwork.InRoom && 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);
- }
- }
- }
- /// <summary>
- /// MonoBehaviour method called on GameObject by Unity on every frame.
- /// </summary>
- void Update()
- {
- // "back" button of phone equals "Escape". quit app if that's pressed
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- QuitApplication();
- }
- }
- #endregion
- #region Photon Callbacks
- public override void OnJoinedRoom()
- {
- // Note: it is possible that this monobehaviour is not created (or active) when OnJoinedRoom happens
- // due to that the Start() method also checks if the local player character was network instantiated!
- 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);
- }
- }
- /// <summary>
- /// Called when a Photon Player got connected. We need to then load a bigger scene.
- /// </summary>
- /// <param name="other">Other.</param>
- 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();
- }
- }
- /// <summary>
- /// Called when a Photon Player got disconnected. We need to load a smaller scene.
- /// </summary>
- /// <param name="other">Other.</param>
- 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();
- }
- }
- /// <summary>
- /// Called when the local player left the room. We need to load the launcher scene.
- /// </summary>
- 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" );
- return;
- }
- Debug.LogFormat( "PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount );
- PhotonNetwork.LoadLevel("PunBasics-Room for "+PhotonNetwork.CurrentRoom.PlayerCount);
- }
- #endregion
- }
- }
|