2026-05-01 05:47:12 +09:00
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 ; // 현재 상태 텍스트
2026-05-01 07:06:29 +09:00
public TMP_InputField nicknameInput ; // 닉네임 입력칸
public TMP_InputField urlInput ; // 이미지 URL 입력칸
2026-05-01 05:47:12 +09:00
public GameObject loading ;
public Button afterLoadButton ;
public GameObject roomPanel ;
2026-05-01 07:06:29 +09:00
// [1번 기능] 현재 접속자 수를 띄울 텍스트 (기존에 선언해두신 변수 활용)
public TextMeshProUGUI currentPlayerText ;
// [2번 기능] 기본 아바타 URL 리스트 (유니티 인스펙터에서도 개수/내용 수정 가능!)
public string [ ] defaultAvatarUrls = new string [ ]
{
"https://scontent-icn2-1.cdninstagram.com/v/t51.82787-19/657402696_17932715739227818_1102219035706293785_n.jpg?stp=dst-jpg_s150x150_tt6&efg=eyJ2ZW5jb2RlX3RhZyI6InByb2ZpbGVfcGljLmRqYW5nby4yMDguYzEifQ&_nc_ht=scontent-icn2-1.cdninstagram.com&_nc_cat=110&_nc_oc=Q6cZ2gEiAmiFEpKqA0_AvOwYhu_V9dtAieTJ6EHLqVEzLZmvyXvRk0RZ7G3n6BO9gVZEKpE&_nc_ohc=X-ML0v44oVcQ7kNvwGmKPj8&_nc_gid=i-J7dc5bYegcPnPIkL9scQ&edm=AP4sbd4BAAAA&ccb=7-5&oh=00_Af7RgrBAKQfl7RpZSpdbPpPbKvVYuz5RYk93sONhI1Yrrw&oe=69F9A119&_nc_sid=7a9f4b" , // 여기에 미리 쓸 URL들을 넣어주세요.
"https://i.namu.wiki/i/0uubPPIF2f9mKDb2fD19gMa77g2rUpoDnQ5Ekb9iqSNea2sfq9u9eWmRUGZFMIOy77XxnSI0HrIfTBj-U-wt4Q.webp" ,
"https://i.namu.wiki/i/Ob0--Jok1pkNtNu46VjPmTZWbmaql5Xf0pexZ5RBz3B5Nlj8z_xqYSyMqw70Ad5mEYa_i1GHcHda5pbilvBNOA.webp"
} ;
2026-05-01 05:47:12 +09:00
void Start ( )
{
afterLoadButton . interactable = false ;
// 핵심: 방장이 씬을 로드하면 나머지 인원도 자동으로 따라가게 설정
PhotonNetwork . AutomaticallySyncScene = true ;
PhotonNetwork . ConnectUsingSettings ( ) ;
}
2026-05-01 07:06:29 +09:00
// [1번 기능] 매 프레임마다 현재 접속자 수를 확인해서 텍스트 업데이트
void Update ( )
{
// 서버에 연결되어 있을 때, 그리고 텍스트 UI가 연결되어 있을 때만 실행
if ( PhotonNetwork . IsConnected & & currentPlayerText ! = null )
{
// PhotonNetwork.CountOfPlayers = 현재 포톤 서버에 접속한 총 플레이어 수
currentPlayerText . text = $"Current Playing: {PhotonNetwork.CountOfPlayers}" ;
}
}
2026-05-01 05:47:12 +09:00
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 ;
2026-05-01 07:06:29 +09:00
// 1. 포톤 서버에 내 닉네임을 공식적으로 등록합니다!
2026-05-01 05:47:12 +09:00
PhotonNetwork . NickName = nicknameInput . text ;
2026-05-01 07:06:29 +09:00
// 2. 아바타 URL 저장 및 [2번 기능] 예외 처리
2026-05-01 05:47:12 +09:00
string myUrl = urlInput . text ;
2026-05-01 07:06:29 +09:00
// 만약 유저가 URL을 입력하지 않았다면?
if ( string . IsNullOrEmpty ( myUrl ) )
{
// 준비해둔 기본 URL 목록 중에서 랜덤으로 하나를 뽑아서 적용!
int randomIndex = Random . Range ( 0 , defaultAvatarUrls . Length ) ;
myUrl = defaultAvatarUrls [ randomIndex ] ;
}
2026-05-01 05:47:12 +09:00
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 ) ; // 일단 게임 시작 버튼은 숨겨놓기
2026-05-01 07:06:29 +09:00
2026-05-01 05:47:12 +09:00
// 내가 방장(처음 방을 만든 사람)이라면 게임 시작 버튼 활성화
if ( PhotonNetwork . IsMasterClient )
{
startButton . gameObject . SetActive ( true ) ;
}
}
// 방장만 누를 수 있는 '게임 시작' 버튼에 연결할 함수
public void ClickStartGame ( )
{
PhotonNetwork . LoadLevel ( "GameScene" ) ;
}
public void ClickLeaveRoom ( )
{
PhotonNetwork . LeaveRoom ( ) ;
statusText . text = "방에서 나가는 중..." ;
}
public override void OnLeftRoom ( )
{
roomPanel . SetActive ( false ) ;
statusText . text = "무사히 도망쳤다!" ;
}
}