GameManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 (PhotonNetwork.InRoom && 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. public override void OnJoinedRoom()
  73. {
  74. // Note: it is possible that this monobehaviour is not created (or active) when OnJoinedRoom happens
  75. // due to that the Start() method also checks if the local player character was network instantiated!
  76. if (PlayerManager.LocalPlayerInstance == null)
  77. {
  78. Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
  79. // we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
  80. PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
  81. }
  82. }
  83. /// <summary>
  84. /// Called when a Photon Player got connected. We need to then load a bigger scene.
  85. /// </summary>
  86. /// <param name="other">Other.</param>
  87. public override void OnPlayerEnteredRoom( Player other )
  88. {
  89. Debug.Log( "OnPlayerEnteredRoom() " + other.NickName); // not seen if you're the player connecting
  90. if ( PhotonNetwork.IsMasterClient )
  91. {
  92. Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
  93. LoadArena();
  94. }
  95. }
  96. /// <summary>
  97. /// Called when a Photon Player got disconnected. We need to load a smaller scene.
  98. /// </summary>
  99. /// <param name="other">Other.</param>
  100. public override void OnPlayerLeftRoom( Player other )
  101. {
  102. Debug.Log( "OnPlayerLeftRoom() " + other.NickName ); // seen when other disconnects
  103. if ( PhotonNetwork.IsMasterClient )
  104. {
  105. Debug.LogFormat( "OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient ); // called before OnPlayerLeftRoom
  106. LoadArena();
  107. }
  108. }
  109. /// <summary>
  110. /// Called when the local player left the room. We need to load the launcher scene.
  111. /// </summary>
  112. public override void OnLeftRoom()
  113. {
  114. SceneManager.LoadScene("PunBasics-Launcher");
  115. }
  116. #endregion
  117. #region Public Methods
  118. public void LeaveRoom()
  119. {
  120. PhotonNetwork.LeaveRoom();
  121. }
  122. public void QuitApplication()
  123. {
  124. Application.Quit();
  125. }
  126. #endregion
  127. #region Private Methods
  128. void LoadArena()
  129. {
  130. if ( ! PhotonNetwork.IsMasterClient )
  131. {
  132. Debug.LogError( "PhotonNetwork : Trying to Load a level but we are not the master Client" );
  133. return;
  134. }
  135. Debug.LogFormat( "PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount );
  136. PhotonNetwork.LoadLevel("PunBasics-Room for "+PhotonNetwork.CurrentRoom.PlayerCount);
  137. }
  138. #endregion
  139. }
  140. }