Unity/Photon

09. Photon - ์‹ค์Šต 1 (7) Room List

๐Ÿ”ท์‹ค์Šต 1 (7)

- ๋ฐฉ ๋ชฉ๋ก ๋„์šฐ๊ธฐ

- ๋ชฉ๋ก์—์„œ ๋ฐฉ์„ ์„ ํƒํ•ด ํ•ด๋‹น ๋ฐฉ์— ์ž…์žฅ

 

๐Ÿ”ถ ๋ฐฉ ๋ชฉ๋ก ๋„์šฐ๊ธฐ

- Scroll View ์ƒ์„ฑ

    - ๋ฒ„ํŠผ ์ƒ์„ฑ : RoomEntity

- ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ : RoomData

    - RoomEntity์— ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€

 

 

 

- Scrollbar Horizontal ์‚ญ์ œ

 

 

 

- Horizontal : ์–ธ์ฒดํฌ

- Horizontal Scrollbar : ์•„๋ฌด๊ฒƒ๋„ ์—ฐ๊ฒฐ ์•ˆ๋˜์–ด ์žˆ๋Š” ๊ฒƒ ํ™•์ธ

 

 

 

- ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€

    - Vertical Layout Group

        - Padding ์กฐ์ ˆ

        - Child Alignment : Upper Center

        - Child Force Expand : ์–ธ์ฒดํฌ

    - Content Size Fitter

        - Vertical Fit : Min Size

 

 

 

- ํ”„๋ฆฌํŒนํ™”

- ํ•˜์ด์–ด๋ผํ‚ค์— ์žˆ๋Š” RoomEntity ์‚ญ์ œ

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using TMPro;

public class RoomData : MonoBehaviour
{
    private TMP_Text RoomInfoText;
    private RoomInfo _roomInfo;

    public TMP_InputField userIdText;

    public RoomInfo RoomInfo
    {
        get
        {
            return _roomInfo;
        }
        set
        {
            _roomInfo = value;
            // EX : room_03 (1/2)
            RoomInfoText.text = $"{_roomInfo.Name} ({_roomInfo.PlayerCount}/{_roomInfo.MaxPlayers})";
            // ๋ฒ„ํŠผ์˜ ํด๋ฆญ ์ด๋ฒคํŠธ์— ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐ
            GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => OnEnterRoom(_roomInfo.Name));
        }
    }

    void Awake()
    {
        RoomInfoText = GetComponentInChildren<TMP_Text>();
        userIdText = GameObject.Find("InputField (TMP) - Nickname").GetComponent<TMP_InputField>();
    }

    void OnEnterRoom(string roomName)
    {
        RoomOptions ro = new RoomOptions();
        ro.IsOpen = true;
        ro.IsVisible = true;
        ro.MaxPlayers = 2;

        PhotonNetwork.NickName = userIdText.text;
        PhotonNetwork.JoinOrCreateRoom(roomName, ro, TypedLobby.Default);
    }
}

 

 

 

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 Dictionary<string, GameObject> roomDict = new Dictionary<string, GameObject>();
    // ๋ฃธ์„ ํ‘œ์‹œํ•  ํ”„๋ฆฌํŒน
    public GameObject roomPrefab;
    // Room ํ”„๋ฆฌํŒน์ด ์ฐจ์ผ๋“œํ™” ์‹œํ‚ฌ ๋ถ€๋ชจ ๊ฐ์ฒด
    public Transform scrollContent;

    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");
        }
    }

    public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        GameObject tempRoom = null;
        foreach (var room in roomList)
        {
            // ๋ฃธ์ด ์‚ญ์ œ๋œ ๊ฒฝ์šฐ
            if (room.RemovedFromList == true)
            {
                roomDict.TryGetValue(room.Name, out tempRoom);
                Destroy(tempRoom);
                roomDict.Remove(room.Name);
            }
            // ๋ฃธ ์ •๋ณด๊ฐ€ ๊ฐฑ์‹ (๋ณ€๊ฒฝ)๋œ ๊ฒฝ์šฐ
            else
            {
                // ๋ฃธ์ด ์ฒ˜์Œ ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ
                if (roomDict.ContainsKey(room.Name) == false)
                {
                    GameObject _room = Instantiate(roomPrefab, scrollContent);
                    _room.GetComponent<RoomData>().RoomInfo = room;
                    roomDict.Add(room.Name, _room);
                }
                // ๋ฃธ ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒฝ์šฐ
                else
                {
                    roomDict.TryGetValue(room.Name, out tempRoom);
                    tempRoom.GetComponent<RoomData>().RoomInfo = room;
                }
            }
        }
    }


    #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();
    }

    // Room ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ (๋ฃธ ์ƒ์„ฑ)
    public void OnMakeRoomClick()
    {
        RoomOptions ro = new RoomOptions();
        ro.IsOpen = true;
        ro.IsVisible = true;
        ro.MaxPlayers = 2;

        // ์ธํ’‹ํ•„๋“œ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด
        if (string.IsNullOrEmpty(roomNameText.text))
        {
            // ๋žœ๋ค ๋ฃธ ์ด๋ฆ„ ๋ถ€์—ฌ
            roomNameText.text = $"ROOM_{Random.Range(1, 100):000}";
        }

        PhotonNetwork.CreateRoom(roomNameText.text, ro);
    }

    #endregion

}

 

 

 

- ํด๋ฆญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ

 

 

 

- play

- USER_52 : Room ๋ฒ„ํŠผ ํด๋ฆญ

 

 

 

- USER_52๊ฐ€ ๋ฐฉ์„ ์ƒ์„ฑ

- ojui_1 : ์ƒ์„ฑ๋œ ๋ฐฉ ํด๋ฆญ

 

 

 

- ๊ฐ™์€ ๋ฐฉ์— ์ž…์žฅ

 

 

 

- ๋ฐฉ๋ชฉ๋ก์— 2/2๋ผ๊ณ  ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์–ด์žˆ์Œ

- ๋ฐฉ์„ ์ƒ์„ฑ

 

 

 

- ๋ฐฉ ๋ชฉ๋ก์— ๋ฐฉ 2๊ฐœ๊ฐ€ ์ƒ๊ธด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ

'Unity > Photon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Photon - VR(XR Toolkit), Voice  (0) 2023.06.14
08. Photon - ์‹ค์Šต 1 (6) Photon Nickname  (0) 2021.08.24
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