본문으로 바로가기

플레이어 수에 맞는 룸 생성(GameMaster.cs)

#region Private Methods

        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); //플레이어의 수에 맞느 룸 배정하기(방장)
        }

        #endregion

방장이 있어야만 룸을 생성하고 로딩할 수 있음

 

플레이어가 방에 들어가거나 나갈 때 구체적인 코드(GameMaster.cs)

#region Photon Callbacks


        /// <summary>
        /// 로컬 플레이어가 방을 나갔을 때 호출됩니다. 런처 장면을 로드해야 합니다.
        /// </summary>
        public override void OnLeftRoom()
        {
            SceneManager.LoadScene(0); //빌드 세팅 0번 씬으로 이동
        }

        public override void OnPlayerEnteredRoom(Player other)
        {
            Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName); // 플레이어가 연결중일 때 뜨지 않음


            if (PhotonNetwork.IsMasterClient)
            {
                Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); //OnPlayerLeftRoom 전에 호출


                LoadArena(); //다른 사람에게도 플레이어가 방을 떠났는 것을 알려준다. 
            }
        }


        public override void OnPlayerLeftRoom(Player other)
        {
            Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName); // 다른 사람의 연결이 끊겼을 때


            if (PhotonNetwork.IsMasterClient) //만약 방장이라면
            {
                Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient); // 방장인 경우에만 룸을 떠나는 사람을 통보해줌.


                LoadArena(); //다른 사람에게도 플레이어가 방을 떠났는 것을 알려준다. 
            }
        }

        #endregion

해당 이름/플레이어 수의 룸을 로딩하는 코드(Launcher.cs)

// #Critical: We only load if we are the first player, else we rely on `PhotonNetwork.AutomaticallySyncScene` to sync our instance scene.
if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
{
    Debug.Log("We load the 'Room for 1' ");


    // #Critical
    // Load the Room Level.
    PhotonNetwork.LoadLevel("Room for 1");
}

플레이어가 1명일 때 1번 방을 호출함, LoadLevel("이동한 씬 이름");

 

방을 떠날 때 씬이 두번 호출되는 버그 수정 코드(Launcher.cs)

/// <summary>
/// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon,
/// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
/// Typically this is used for the OnConnectedToMaster() callback.
/// </summary>
bool isConnecting;

#region PrivateField영역에 작성

 

// keep track of the will to join a room, because when we come back from the game we will get a callback that we are connected, so we need to know what to do then
isConnecting = true;

Connect()함수 안에 작성 

 

// we don't want to do anything if we are not attempting to join a room.
// this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
// we don't want to do anything.
if (isConnecting)
{
    // #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
    PhotonNetwork.JoinRandomRoom();
}

OnConnectedToMaster() 함수 안에 작성, 기존의 PhotonNetwork.JoinRandomRoom()명령을 삭제하고 해당 코드를 추가