๐ท ์ค์ต 1 (6)
- ๋๋ค์ ์ค์
๐ถ ๋๋ค์ ์ค์
- UI ์ธํ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using TMPro;
public class PhotonManager : MonoBehaviourPunCallbacks
{
private readonly string gameVersion = "v1.0";
private string userId = "Ojui";
public TMP_InputField userIdText;
public TMP_InputField roomNameText;
private void Awake()
{
// ๋ฐฉ์ฅ์ด ํผ์ ์ฌ์ ๋ก๋ฉํ๋ฉด, ๋๋จธ์ง ์ฌ๋๋ค์ ์๋์ผ๋ก ์ฑํฌ๊ฐ ๋จ
PhotonNetwork.AutomaticallySyncScene = true;
// ๊ฒ์ ๋ฒ์ ์ง์
PhotonNetwork.GameVersion = gameVersion;
// ์๋ฒ ์ ์
PhotonNetwork.ConnectUsingSettings();
}
void Start()
{
Debug.Log("00. ํฌํค ๋งค๋์ ์์");
userId = PlayerPrefs.GetString("USER_ID", $"USER_{Random.Range(0, 100):00}");
userIdText.text = userId;
PhotonNetwork.NickName = userId;
}
public override void OnConnectedToMaster()
{
Debug.Log("01. ํฌํค ์๋ฒ์ ์ ์");
//๋ก๋น์ ์ ์
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
Debug.Log("02. ๋ก๋น์ ์ ์");
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("๋๋ค ๋ฃธ ์ ์ ์คํจ");
// ๋ฃธ ์์ฑ ์ค์
RoomOptions ro = new RoomOptions();
ro.IsOpen = true;
ro.IsVisible = true;
ro.MaxPlayers = 2;
roomNameText.text = $"Room_{Random.Range(1, 100):000}";
// ๋ฃธ์ ์์ฑ > ์๋ ์
์ฅ๋จ
PhotonNetwork.CreateRoom("room_1", ro);
}
public override void OnCreatedRoom()
{
Debug.Log("03. ๋ฐฉ ์์ฑ ์๋ฃ");
}
public override void OnJoinedRoom()
{
Debug.Log("04. ๋ฐฉ ์
์ฅ ์๋ฃ");
if (PhotonNetwork.IsMasterClient)
{
PhotonNetwork.LoadLevel("Level_1");
}
}
#region UI_BUTTON_CALLBACK
// Random ๋ฒํผ ํด๋ฆญ
public void OnRandomBtn()
{
// ID ์ธํํ๋๊ฐ ๋น์ด์์ผ๋ฉด
if (string.IsNullOrEmpty(userIdText.text))
{
// ๋๋ค ์์ด๋ ๋ถ์ฌ
userId = $"USER_{Random.Range(0, 100):00}";
userIdText.text = userId;
}
PlayerPrefs.SetString("USER_ID", userIdText.text);
PhotonNetwork.NickName = userIdText.text;
PhotonNetwork.JoinRandomRoom();
}
#endregion
}
- userId = PlayerPrefs.GetString("USER_ID", $"USER_{Random.Range(0, 100):00}");
- PlayerPrefs์์ USER_ID ํค์ ๋์๋๋ ๊ฐ์ ๊ฐ์ ธ์ด
- USER_ID ํค๊ฐ ์์ผ๋ฉด USER_{Random.Range(0, 100):00} ๊ฐ์ return
- ์ฐ๊ฒฐ
- ํด๋ฆญ ์ด๋ฒคํธ ์ฐ๊ฒฐ
- play
- Nickname์ ๋๋ค ๊ฐ์ด ๋ค์ด๊ฐ
- Random ๋ฒํผ์ ๋๋ฅด๋ฉด Level_1์ฌ์ ๋ถ๋ฌ์ด
- Player์ PhotonView๋ฅผ ๋ณด๋ฉด Nickname์ด ๋ณ๊ฒฝ๋ ๊ฒ์ ํ์ธํ ์ ์์
'Unity > Photon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Photon - VR(XR Toolkit), Voice (0) | 2023.06.14 |
---|---|
09. Photon - ์ค์ต 1 (7) Room List (6) | 2021.08.25 |
07. Photon - ์ค์ต 1 (5) Photon Voice (0) | 2021.08.23 |
06. Photon - ์ค์ต 1 (4) Lobby, Custom Property (0) | 2021.08.22 |
05. Photon - ์ค์ต 1 (3) Score (1) | 2021.08.18 |