-
07/20 포톤 서버 사용게임 네트워크 프로그래밍 2021. 7. 20. 17:58
https://www.photonengine.com/ko-KR/
글로벌 크로스 플랫폼 실시간 게임 개발 | Photon Engine
멀티플레이 기능 추가와 글로벌 전개가 Photon Cloud로 간단하게 실현됩니다! Photon Realtime SDK는 모든 Photon Cloud 서비스에 엑세스하는 효율적이고 중요한 API이며, PUN, BOLT, QUANTUM 등 보다 높은 레벨의
www.photonengine.com
https://github.com/IJEMIN/Unity-Programming-Essence
IJEMIN/Unity-Programming-Essence
레트로의 유니티 게임 프로그래밍 에센스. Contribute to IJEMIN/Unity-Programming-Essence development by creating an account on GitHub.
github.com
https://assetstore.unity.com/packages/tools/network/pun-2-free-119922
PUN 2 - FREE | 네트워크 | Unity Asset Store
Get the PUN 2 - FREE package from Exit Games and speed up your game development process. Find this & other 네트워크 options on the Unity Asset Store.
assetstore.unity.com
깃 허브에서 받은 프로젝트
18/Zombie Multiplayer 열기
PUN 2 - FREE 임포트
포톤 홈페이지 가입 후 로그인
관리화면에서 새 어플리케이션 만들기
어플리케이션ID 복사
펀 위자드에 붙여넣기 후 셋업 프로젝트 클릭
게임 실행과 동시에 마스터 서버 접속 시도
PhotonNetwork.GameVersion = this.gameVersion;
PhotonNetwork.ConnectUsingSettings();LobbyManager.cs
using Photon.Pun; // 유니티용 포톤 컴포넌트들 using Photon.Realtime; // 포톤 서비스 관련 라이브러리 using UnityEngine; using UnityEngine.UI; // 마스터(매치 메이킹) 서버와 룸 접속을 담당 public class LobbyManager : MonoBehaviourPunCallbacks { private string gameVersion = "1"; // 게임 버전 public Text connectionInfoText; // 네트워크 정보를 표시할 텍스트 public Button joinButton; // 룸 접속 버튼 // 게임 실행과 동시에 마스터 서버 접속 시도 private void Start() { PhotonNetwork.GameVersion = this.gameVersion; PhotonNetwork.ConnectUsingSettings(); this.joinButton.interactable = false; this.connectionInfoText.text = "마스터서버에 접속"; } // 마스터 서버 접속 성공시 자동 실행 public override void OnConnectedToMaster() { this.joinButton.interactable = true; this.connectionInfoText.text = "온라인: 마스터 서버와 연결됨"; } // 마스터 서버 접속 실패시 자동 실행 public override void OnDisconnected(DisconnectCause cause) { this.joinButton.interactable = true; this.connectionInfoText.text = "오프라인: 마스터 서버와 연결되지 않음"; PhotonNetwork.ConnectUsingSettings(); } // 룸 접속 시도 public void Connect() { } // (빈 방이 없어)랜덤 룸 참가에 실패한 경우 자동 실행 public override void OnJoinRandomFailed(short returnCode, string message) { } // 룸에 참가 완료된 경우 자동 실행 public override void OnJoinedRoom() { } }
아래 사진처럼 일부 프리팹 리소스폴더에 이동
설정 확인
LobbyManager.cs
using Photon.Pun; // 유니티용 포톤 컴포넌트들 using Photon.Realtime; // 포톤 서비스 관련 라이브러리 using UnityEngine; using UnityEngine.UI; // 마스터(매치 메이킹) 서버와 룸 접속을 담당 public class LobbyManager : MonoBehaviourPunCallbacks { private string gameVersion = "1"; // 게임 버전 public Text connectionInfoText; // 네트워크 정보를 표시할 텍스트 public Button joinButton; // 룸 접속 버튼 // 게임 실행과 동시에 마스터 서버 접속 시도 private void Start() { //접속에 필요한 정보 (게임 버전) 설정 PhotonNetwork.GameVersion = this.gameVersion; //설정한 정보로 마스터 서버 접속 시도 PhotonNetwork.ConnectUsingSettings(); this.joinButton.interactable = false; this.connectionInfoText.text = "마스터서버에 접속중..."; } // 마스터 서버 접속 성공시 자동 실행 public override void OnConnectedToMaster() { this.joinButton.interactable = true; this.connectionInfoText.text = "온라인: 마스터 서버와 연결됨"; } // 마스터 서버 접속 실패시 자동 실행 public override void OnDisconnected(DisconnectCause cause) { this.joinButton.interactable = false; this.connectionInfoText.text = "오프라인: 마스터 서버와 연결되지 않음\n접속 재시도중..."; //설정한 정보로 마스터 서버 접속 시도 PhotonNetwork.ConnectUsingSettings(); } // 룸 접속 시도 public void Connect() { //중복 접속 막기 this.joinButton.interactable = false; //마스터 서버에 접속 중이라면 if (PhotonNetwork.IsConnected) { //룸에 접속한다 this.connectionInfoText.text = "룸에 접속..."; PhotonNetwork.JoinRandomRoom(); } else { this.connectionInfoText.text = "오프라인 : 마스터 서버와 연결끊김\n접속 재시도..."; PhotonNetwork.ConnectUsingSettings(); } } // (빈 방이 없어)랜덤 룸 참가에 실패한 경우 자동 실행 public override void OnJoinRandomFailed(short returnCode, string message) { this.connectionInfoText.text = "빈 방 없음, 새로운방 생성..."; //최대 인원을 4명으로 설정 + 방을 만듬 PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = 4 }); } // 룸에 참가 완료된 경우 자동 실행 public override void OnJoinedRoom() { this.connectionInfoText.text = "방 참가 성공!"; //모든 룸 참가자가 Main 씬을 로드 하게 함 PhotonNetwork.LoadLevel("Main"); } }
코드 수정 후 빌드
멀티
포톤 테스트
참고
https://cafe.naver.com/gameprogramming7/472
메시지 RPC전송 (채팅비슷하게)
cafe.naver.com
'게임 네트워크 프로그래밍' 카테고리의 다른 글
07/27 nodejs + Unity연동 (UnityWebRequest) (0) 2021.07.27 07/26 Assetbundle 에셋번들 (0) 2021.07.26