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 | using System; using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; using Photon.Pun; using Photon.Realtime; namespace Com.MyCompany.MyGame { public class GameManager : MonoBehaviourPunCallbacks { public override void OnLeftRoom() { SceneManager.LoadScene(0); } public void LeaveRoom() { PhotonNetwork.LeaveRoom(); } private 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); if (PhotonNetwork.IsMasterClient) { Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); LoadArena(); } } public override void OnPlayerLeftRoom(Player other) { Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName); if (PhotonNetwork.IsMasterClient) { Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); LoadArena(); } } } } | cs |
GameManager 스크립트에 OnPlayerEnteredRoom 메소드로 현재 플레이 인원에 따라 만들어둔 씬을 로드하게 하자
** PhotonNetwork.LoadLevel() 는 MasterClient 만이 호출가능하다
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 74 75 76 77 78 79 80 81 82 83 84 85 86 | using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; using Photon.Realtime; namespace Com.MyCompany.MyGame { public class Launcher : MonoBehaviourPunCallbacks { [SerializeField] private GameObject controlPanel; [Tooltip("The UI Label to Inform the user that the connection is in progress")] [SerializeField] private GameObject progressLabel; string gameVersion = "1"; bool isConnecting=false; private void Awake() { PhotonNetwork.AutomaticallySyncScene = true; } void Start() { progressLabel.SetActive(false); controlPanel.SetActive(true); } void Update() { } public void Connect() { isConnecting = true; if (PhotonNetwork.IsConnected) { PhotonNetwork.JoinRandomRoom(); } else { PhotonNetwork.GameVersion = gameVersion; PhotonNetwork.ConnectUsingSettings(); } progressLabel.SetActive(true); controlPanel.SetActive(false); } public override void OnConnectedToMaster() { Debug.Log("PUN Basics Tutorial/Launvher: OnConnectedToMaster() was called by PUN"); if (isConnecting) { PhotonNetwork.JoinRandomRoom(); } } public override void OnJoinRandomFailed(short returnCode, string message) { Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom"); PhotonNetwork.CreateRoom(null, new RoomOptions()); } public override void OnDisconnected(DisconnectCause cause) { Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause); } public override void OnJoinedRoom() { Debug.Log("OnJoinedRoom Event"); if (PhotonNetwork.CurrentRoom.PlayerCount == 1) { Debug.Log("We load the 'Room for 1' "); PhotonNetwork.LoadLevel("Room for 1"); } } } } | cs |
Launcher 스크립트에 PhotonNetwork.JoinRandomRoom() 으로 랜던룸에 입장을 시도 할때 방이없으면 접속에 실패하게되는데 해당 코드를 작성하지 않았다
OnJoinRandomFailed 콜백 메소드를 통하여 방이없을 때 방을 만들어보자
이 후 서버에 연결을 해보면 어제 만든 씬으로 이동하는 것을 볼 수 있다
대강 접속로직 알것같다
'해보자 > 포톤을 배워보자' 카테고리의 다른 글
8일차 - 동기화를 위한 포톤 컴포넌트 (0) | 2019.01.23 |
---|---|
7일차 - 캐릭터 프리팹을 만들어보자 (0) | 2019.01.20 |
5일차 포톤 튜토리얼 계속계속 해보자 (0) | 2019.01.15 |
4일차 - 포톤 튜토리얼 계속 해보자 (0) | 2019.01.14 |
3일차 - PUN으로 클라우드에 접속보자 (0) | 2019.01.13 |