using System.Collections; using UnityEngine; using Photon.Pun; using Photon.Realtime; using TMPro; // UI 텍스트용 using UnityEngine.UI; using Hashtable = ExitGames.Client.Photon.Hashtable; // UI 버튼용 public class LobbyManager : MonoBehaviourPunCallbacks { public TMP_InputField roomCodeInput; // 방 코드 입력칸 public Button joinButton; // 접속 버튼 public Button startButton; // 게임 시작 버튼 (방장 전용) public TextMeshProUGUI statusText; // 현재 상태 텍스트 public TMP_InputField nicknameInput; // [추가] 닉네임 입력칸 public TMP_InputField urlInput; public GameObject loading; public Button afterLoadButton; public GameObject roomPanel; void Start() { afterLoadButton.interactable = false; // 핵심: 방장이 씬을 로드하면 나머지 인원도 자동으로 따라가게 설정 PhotonNetwork.AutomaticallySyncScene = true; PhotonNetwork.ConnectUsingSettings(); } public override void OnConnectedToMaster() { loading.SetActive(false); afterLoadButton.interactable = true; PhotonNetwork.JoinLobby(); } // UI 버튼 클릭 시 호출할 함수 public void ClickJoinRoom() { // 방 코드나 닉네임이 비어있으면 안 넘어감 if (string.IsNullOrEmpty(roomCodeInput.text) || string.IsNullOrEmpty(nicknameInput.text)) return; // 1. [핵심] 포톤 서버에 내 닉네임을 공식적으로 등록합니다! PhotonNetwork.NickName = nicknameInput.text; // 2. 아바타 URL 저장 (기존과 동일) string myUrl = urlInput.text; Hashtable myProps = new Hashtable() { { "AvatarUrl", myUrl } }; PhotonNetwork.LocalPlayer.SetCustomProperties(myProps); // 3. 방 접속 RoomOptions roomOptions = new RoomOptions { MaxPlayers = 10 }; PhotonNetwork.JoinOrCreateRoom(roomCodeInput.text, roomOptions, TypedLobby.Default); statusText.text = "방 접속 중..."; } public override void OnJoinedRoom() { statusText.text = $"방 입장 완료! (코드: {PhotonNetwork.CurrentRoom.Name})"; StartCoroutine(RoomPanel()); } private IEnumerator RoomPanel() { yield return new WaitForSecondsRealtime(2f); roomPanel.SetActive(true); startButton.gameObject.SetActive(false); // 일단 게임 시작 버튼은 숨겨놓기 // 내가 방장(처음 방을 만든 사람)이라면 게임 시작 버튼 활성화 if (PhotonNetwork.IsMasterClient) { startButton.gameObject.SetActive(true); } } // 방장만 누를 수 있는 '게임 시작' 버튼에 연결할 함수 public void ClickStartGame() { // "GameScene"이라는 이름의 씬을 불러옴 (모두가 다 같이 넘어갑니다!) PhotonNetwork.LoadLevel("GameScene"); } public void ClickLeaveRoom() { // 포톤 서버에 "나 이 방에서 나갈래!" 라고 요청합니다. PhotonNetwork.LeaveRoom(); statusText.text = "방에서 나가는 중..."; } // [추가 2] 방에서 완전히 빠져나왔을 때 포톤이 자동으로 실행해 주는 콜백 함수 public override void OnLeftRoom() { // 방에서 나왔으니, UI를 다시 처음 상태(로그인 화면)로 되돌립니다. roomPanel.SetActive(false); statusText.text = "무사히 도망쳤다!"; } }