87 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using UnityEngine;
 | |
| using UnityEngine.SceneManagement;
 | |
| 
 | |
| using Photon.Pun;
 | |
| using Photon.Realtime;
 | |
| 
 | |
| namespace cc.ivanli.pun01
 | |
| {
 | |
|     public class GameManager : MonoBehaviourPunCallbacks
 | |
|     {
 | |
|         public static GameManager Instance;
 | |
| 
 | |
|         [Tooltip("The prefab to use for representing the player")]
 | |
|         public GameObject playerPrefab;
 | |
| 
 | |
|         [System.Obsolete]
 | |
|         void Start()
 | |
|         {
 | |
|             Instance = this;
 | |
| 
 | |
|             if (!playerPrefab)
 | |
|             {
 | |
|                 Debug.LogError("<Color=Red><a>Missing</a></Color> playerPrefab Reference. Please set it up in GameObject 'Game Manager'", this);
 | |
|             }
 | |
|             else if (!PlayerManager.LocalPlayerInstance)
 | |
|             {
 | |
|                 Debug.LogFormat("We are Instantiating LocalPlayer from {0}", Application.loadedLevelName);
 | |
|                 // 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);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public override void OnLeftRoom()
 | |
|         {
 | |
|             base.OnLeftRoom();
 | |
|             SceneManager.LoadScene(0);
 | |
|         }
 | |
| 
 | |
|         public void LeaveRoom()
 | |
|         {
 | |
|             PhotonNetwork.LeaveRoom();
 | |
|         }
 | |
| 
 | |
|         public 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("Room for " + PhotonNetwork.CurrentRoom.PlayerCount);
 | |
|         }
 | |
| 
 | |
|         public override void OnPlayerEnteredRoom(Player other)
 | |
|         {
 | |
|             Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName); // not seen if you're the player connecting
 | |
| 
 | |
| 
 | |
|             if (PhotonNetwork.IsMasterClient)
 | |
|             {
 | |
|                 Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
 | |
| 
 | |
| 
 | |
|                 LoadArena();
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public override void OnPlayerLeftRoom(Player other)
 | |
|         {
 | |
|             Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName); // seen when other disconnects
 | |
| 
 | |
| 
 | |
|             if (PhotonNetwork.IsMasterClient)
 | |
|             {
 | |
|                 Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // called before OnPlayerLeftRoom
 | |
| 
 | |
| 
 | |
|                 LoadArena();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } |