VioletaBabel
58. Photon Tutorial (4) 본문
GameManager.cs를 다음과 같이 수정한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //GameManager.cs using System.Collections; using System; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { private void LoadArena() { if (!PhotonNetwork.isMasterClient) // 마스터 클라이언트인지 확인. Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client"); Debug.Log("PhotonNetwork : Loading Level : " + PhotonNetwork.room.PlayerCount); PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.room.PlayerCount); //네트워크 메세지 큐를 잠시 멈추기 위해 //레벨 로딩을 Wrap함. //이 메소드는 PhotonNetwork.isMessageQueueRunning을 false함. //그리고 레벨 로딩이 끝나면 enable. //룸 내의 로드된 레벨과 동기화를 위하여 //PhotonNetwork.automaticallySyncScene을 사용하도록 하였기에 //룸 안의 모든 클라이언트의 레벨 로드를 유니티가 아닌 Photon이 하도록 함. //마스터인 경우에만 호출되어야 함. } public void OnLeftRoom() { SceneManager.LoadScene(0); // Photon Callback인 OnLeftRoom을 listen하여 0번 Scene인 Launcher를 로드한다. } public void LeaveRoom() { PhotonNetwork.LeaveRoom(); // 현재 참여하고 있는 룸을 떠나 마스터 서버로 되돌아간다. // autoCleanUp을 false로 하지 않았을 경우, PhotonView의 모든 네트워크 게임 오브젝트를 제거. // 마스터 서버도 되돌아가며, 오프라인 모드에서는 OnLeftRoom이 곧바로 호출됨. // 추상화를 위해 public 메소드로 wrap 함. } } | cs |
이제 원하는 레벨을 로드하는 기능이 생겼다.
다음은 플레이어들의 접속과 접속 해제를 Listen하여보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | //GameManager.cs using System.Collections; using System; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : Photon.PunBehaviour { private void LoadArena() { if (!PhotonNetwork.isMasterClient) // 마스터 클라이언트인지 확인. Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client"); Debug.Log("PhotonNetwork : Loading Level : " + PhotonNetwork.room.PlayerCount); PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.room.PlayerCount); //네트워크 메세지 큐를 잠시 멈추기 위해 //레벨 로딩을 Wrap함. //이 메소드는 PhotonNetwork.isMessageQueueRunning을 false함. //그리고 레벨 로딩이 끝나면 enable. //룸 내의 로드된 레벨과 동기화를 위하여 //PhotonNetwork.automaticallySyncScene을 사용하도록 하였기에 //룸 안의 모든 클라이언트의 레벨 로드를 유니티가 아닌 Photon이 하도록 함. //마스터인 경우에만 호출되어야 함. } public void OnLeftRoom() { SceneManager.LoadScene(0); // Photon Callback인 OnLeftRoom을 listen하여 0번 Scene인 Launcher를 로드한다. } public void LeaveRoom() { PhotonNetwork.LeaveRoom(); // 현재 참여하고 있는 룸을 떠나 마스터 서버로 되돌아간다. // autoCleanUp을 false로 하지 않았을 경우, PhotonView의 모든 네트워크 게임 오브젝트를 제거. // 마스터 서버도 되돌아가며, 오프라인 모드에서는 OnLeftRoom이 곧바로 호출됨. // 추상화를 위해 public 메소드로 wrap 함. } public override void OnPhotonPlayerConnected(PhotonPlayer other) { //원격 플레이어가 룸에 들어왔을 때 호출. PhotonPlayer가 playerlist에 이미 추가된 시점에 불림. //특정 수의 플레이어로 게임이 시작 시, 이 콜백 내에서 Room.PlayerCount를 체크해 게임 시작이 가능한지 확인하기도 함. Debug.Log("OnPhotonPlayerConnected() " + other.NickName); if(PhotonNetwork.isMasterClient) { Debug.Log("OnPhotonPlayerConnected isMasterClient" + PhotonNetwork.isMasterClient); LoadArena(); } } public override void OnPhotonPlayerDisconnected(PhotonPlayer other) { //원격 플레이어가 룸을 나갔을 때 호출. PhotonPlayer가 playerlist에서 이미 제거된 시점에 불림. //클라이언트가 PhotonNetwork.leaveRoom을 호출할 때 이 메소드를 호출함. //원격 클라이언트의 연결이 끊어져도 이 콜백이 몇 초간의 타임아웃 후 실행 됨. Debug.Log("OnPhotonPlayerDisconnected() " + other.NickName); if(PhotonNetwork.isMasterClient) { Debug.Log("OnPhotonPlayerConnected is MasterClient " + PhotonNetwork.isMasterClient); LoadArena(); } } } | cs |
플레이어가 방에 들어오거나 나가는 경우 MasterClient인 경우에만 LoadArena()가 호출된다.
Launcher 씬을 켜보자.
그리고 Launcher.cs를 수정.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | //Launcher.cs using UnityEngine; public class Launcher : Photon.PunBehaviour { //이 클라이언트의 게임 버전. 사용자는 게임 버전으로 구분됨. string _gameVersion = "1"; public PhotonLogLevel Loglevel = PhotonLogLevel.Informational; // 밑에서 한 것과 다르게, 이렇게 하면 인스펙터에 노출. public byte MaxPlayersPerRoom = 4; // 윗 줄처럼 얘도 이렇게 처리. public GameObject controlPanel; public GameObject progressLabel; private bool isConnecting; private void Awake() { //PhotonNetwork.logLevel = PhotonLogLevel.Full; //네트워크의 로그 레벨을 최대로 올림. //예상하는대로 동작한다는 확신이 서면 //PhotonLogLevel.Informational로 바꾸는 게 좋음. PhotonNetwork.logLevel = Loglevel; //Loglevel을 위 public에서 받아오게 하자. 인스펙터에서 건들 수 있다. PhotonNetwork.autoJoinLobby = false; //마스터 서버에 접속 시 로비에 참여해야 하는지 정의. //false면 마스터에 접속 시 OnConnectedToMaster()가 호출되고 //OnJoinedLobby()는 호출되지 않음. 디폴트 값은 true. PhotonNetwork.automaticallySyncScene = true; //마스터 클라이언트와 같은 레벨로 룸의 모든 클라이언트를 로드하는지. //로드된 레벨을 동기화 해야하면 마스터 클라이언트는 PhotonNetwork.LoadLevel()을 사용. //모든 클라이언트는 갱신을 받거나 참여했을 때 새로운 씬을 로드. } private void Start() { progressLabel.SetActive(false); controlPanel.SetActive(true); } public void Connect() { isConnecting = true; progressLabel.SetActive(true); controlPanel.SetActive(false); if (PhotonNetwork.connected) PhotonNetwork.JoinRandomRoom();//현재 사용 중인 로비에서 사용 가능한 룸에 참여, 룸이 없으면 실패. else PhotonNetwork.ConnectUsingSettings(_gameVersion); //에디터에서 설정된 Photon에 연결. 오프라인 사용 불가. //유효한 AppID가 설정 파일 내에 있어야 함. //게임이 네트워크를 통하여 포톤클라우드로 연결되는 시작 지점. } public override void OnConnectedToMaster() { if (isConnecting) { Debug.Log("DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN"); PhotonNetwork.JoinRandomRoom(); } } public override void OnDisconnectedFromPhoton() { progressLabel.SetActive(false); controlPanel.SetActive(true); Debug.Log("DemoAnimator/Launcher: OnDisconnectedFromPhoton() was called by PUN"); } public override void OnPhotonRandomJoinFailed(object[] codeAndMsg) { Debug.Log("DemoAnimator/Launcher: OnPhotonRandomJoinFailed() was called by PUN. No random room available, " + "so we create one.\nCalling: PhotonNetwork.CreateRoom(null, new RoomOption() {maxPlayers = 4},null)"); PhotonNetwork.CreateRoom(null, new RoomOptions() { maxPlayers = MaxPlayersPerRoom }, null); } public override void OnJoinedRoom() { Debug.Log("DemoAnimator/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room."); if(PhotonNetwork.room.PlayerCount.Equals(1)) { Debug.Log("We load the 'Room for 1' "); PhotonNetwork.LoadLevel("Room for 1"); } } } | cs |
이제 Launcher 씬에서 Play를 누르면 room으로 들어가며, leave하면 다시 첫 화면으로 돌아온다.
'BCA > 8. Photon' 카테고리의 다른 글
60. Photon Tutorial (6) (0) | 2018.08.06 |
---|---|
59. Photon Tutorial (5) (0) | 2018.08.06 |
57. Photon Tutorial (3) (0) | 2018.08.06 |
56. Photon Tutorial (2) (0) | 2018.08.06 |
55. Photon Tutorial (1) (0) | 2018.08.06 |