GameManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Launcher.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Used in "PUN Basic tutorial" to handle typical game management requirements
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using Photon.Realtime;
  13. namespace Photon.Pun.Demo.PunBasics
  14. {
  15. #pragma warning disable 649
  16. /// <summary>
  17. /// Game manager.
  18. /// Connects and watch Photon Status, Instantiate Player
  19. /// Deals with quiting the room and the game
  20. /// Deals with level loading (outside the in room synchronization)
  21. /// </summary>
  22. public class GameManager : MonoBehaviourPunCallbacks
  23. {
  24. #region Public Fields
  25. static public GameManager Instance;
  26. #endregion
  27. #region Private Fields
  28. private GameObject instance;
  29. [Tooltip("The prefab to use for representing the player")]
  30. [SerializeField]
  31. private GameObject playerPrefab;
  32. #endregion
  33. #region MonoBehaviour CallBacks
  34. /// <summary>
  35. /// MonoBehaviour method called on GameObject by Unity during initialization phase.
  36. /// </summary>
  37. void Start()
  38. {
  39. Instance = this;
  40. // in case we started this demo with the wrong scene being active, simply load the menu scene
  41. if (!PhotonNetwork.IsConnected)
  42. {
  43. SceneManager.LoadScene("PunBasics-Launcher");
  44. return;
  45. }
  46. if (playerPrefab == null) { // #Tip Never assume public properties of Components are filled up properly, always check and inform the developer of it.
  47. Debug.LogError("<Color=Red><b>Missing</b></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
  48. } else {
  49. if (PlayerManager.LocalPlayerInstance==null)
  50. {
  51. Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
  52. // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
  53. PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f,5f,0f), Quaternion.identity, 0);
  54. }else{
  55. Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// MonoBehaviour method called on GameObject by Unity on every frame.
  61. /// </summary>
  62. void Update()
  63. {
  64. // "back" button of phone equals "Escape". quit app if that's pressed
  65. if (Input.GetKeyDown(KeyCode.Escape))
  66. {
  67. QuitApplication();
  68. }
  69. }
  70. #endregion
  71. #region Photon Callbacks
  72. /// <summary>
  73. /// Called when a Photon Player got connected. We need to then load a bigger scene.
  74. /// </summary>
  75. /// <param name="other">Other.</param>
  76. public override void OnPlayerEnteredRoom( Player other )
  77. {
  78. Debug.Log( "OnPlayerEnteredRoom() " + other.NickName); // not seen if you're the player connecting
  79. if ( PhotonNetwork.IsMasterClient )
  80. {
  81. Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
  82. LoadArena();
  83. }
  84. }
  85. /// <summary>
  86. /// Called when a Photon Player got disconnected. We need to load a smaller scene.
  87. /// </summary>
  88. /// <param name="other">Other.</param>
  89. public override void OnPlayerLeftRoom( Player other )
  90. {
  91. Debug.Log( "OnPlayerLeftRoom() " + other.NickName ); // seen when other disconnects
  92. if ( PhotonNetwork.IsMasterClient )
  93. {
  94. Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
  95. LoadArena();
  96. }
  97. }
  98. /// <summary>
  99. /// Called when the local player left the room. We need to load the launcher scene.
  100. /// </summary>
  101. public override void OnLeftRoom()
  102. {
  103. SceneManager.LoadScene("PunBasics-Launcher");
  104. }
  105. #endregion
  106. #region Public Methods
  107. public void LeaveRoom()
  108. {
  109. PhotonNetwork.LeaveRoom();
  110. }
  111. public void QuitApplication()
  112. {
  113. Application.Quit();
  114. }
  115. #endregion
  116. #region Private Methods
  117. void LoadArena()
  118. {
  119. if ( ! PhotonNetwork.IsMasterClient )
  120. {
  121. Debug.LogError( "PhotonNetwork : Trying to Load a level but we are not the master Client" );
  122. }
  123. Debug.LogFormat( "PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount );
  124. PhotonNetwork.LoadLevel("PunBasics-Room for "+PhotonNetwork.CurrentRoom.PlayerCount);
  125. }
  126. #endregion
  127. }
  128. }