본문 바로가기
개발/Photon

유니티 C# 포톤 커스텀 프로퍼티 간단 사용법 Photon Custom Property

by SPNK 2022. 12. 22.
반응형
  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using Hashtable = ExitGames.Client.Photon.Hashtable;

public class NetworkManager : MonoBehaviourPunCallbacks
{
    public override void OnJoinedRoom()
    {
        statusText.text = "방에 참가하였습니다.";

        if (PhotonNetwork.IsMasterClient)
        {
            PhotonNetwork.CurrentRoom.SetCustomProperties(new Hashtable() { { "RoomState", "Waiting" } });
            PhotonNetwork.CurrentRoom.SetCustomProperties(new Hashtable() { { "Master", PlayerPrefs.GetString("NickName") } });
            PhotonNetwork.CurrentRoom.SetCustomProperties(new Hashtable() { { "Number", 123 } });
        }
    }

    void LoadRoomState() //방 상태 불러오기
    {
        if (PhotonNetwork.InRoom)
        {
            Hashtable ht = PhotonNetwork.CurrentRoom.CustomProperties;

            switch (ht["RoomState"])
            {
                case "Waiting":
                    Debug.Log("방이 대기중입니다.");
                    break;
                case "Play":
                    Debug.Log("방이 게임중입니다. 다음 게임부터 참여 가능합니다.");
                    break;
                default:

                    break;
            }
        }
    }

    void UpdateRoomState() //방 상태 변경하기
    {
        if(PhotonNetwork.InRoom)
        {
            if(PhotonNetwork.IsMasterClient)
            {
                PhotonNetwork.CurrentRoom.SetCustomProperties(new Hashtable() { { "RoomState", "Playing" } });
            }
        }
    }
}
반응형

댓글