본문 바로가기

유니티/모바일 멀티플레이 Shooting Game

유니티 Photon 사용법 - 방만들기, 방 참가

반응형

로비 UI는 본인 편한대로 보기 좋은대로 만들면 된다.

나는 뭐 별로 이쁘게 할 생각도 없고 딱 중요한 기능들만 넣고 심플한걸 선호해서 이렇게 했다(사실 강의에서도 이렇게함)

 

 

접속화면
접속이 끝난 화면

별건 없고 그냥 눈에 보이는대로다. 버튼들 옆은 Input Text Field로 만들어주자.

그리고 인스펙터창에서 볼 수 있듯이 photon서버에 연결이 되면 ConnecctUI는 꺼주고 RoomUI는 켜주도록한다.

 

 

각 오브젝트들은 위와 같이 직접 드래그 앤 드롭으로 참조 시켜줬다.

코드설명은 주석에 열심히 달아놨다.

 

LobbyManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

using Photon.Pun;
using Photon.Realtime;

public class LobbyManager : MonoBehaviourPunCallbacks//for using PUN2 Network callback
{
   [Header("---UI Screens---")]
   public GameObject roomUI;
   public GameObject connectUI;

   [Header("---UI Text---")]
   public TMP_Text statusText;
   public TMP_Text connectingText;

    [Header("---UI InputFields---")]
    public TMP_InputField createRoom;
    public TMP_InputField joinRoom;

    private void Awake() 
    {
        PhotonNetwork.ConnectUsingSettings();
    }//Connect to PhotonNetwork based on UsingSetting
    //You can check your UsingSetting on your asset folder
    
    public override void OnConnectedToMaster()
    {
        connectingText.text="Joining Lobby...";
        PhotonNetwork.JoinLobby(TypedLobby.Default);
    }// Called when the client is connected to the Master Server and ready for matchmaking and other tasks.
    public override void OnJoinedLobby()
    {
        connectUI.SetActive(false);
        roomUI.SetActive(true);
        statusText.text="Joined To Lobby...";
    }// Called on entering a lobby on the Master Server. The actual room-list updates will call OnRoomListUpdate.
    
    public override void OnJoinedRoom()
    {
        PhotonNetwork.LoadLevel(1);
        //Loading the index 1 scene
    }

    public override void OnDisconnected(DisconnectCause cause)
    {
        connectUI.SetActive(true);
        roomUI.SetActive(false);
        connectingText.text="Disconnected..."+cause.ToString();
    }//If fail to connect...
    
    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        int roomName=Random.Range(0,10000);
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers=4;
        PhotonNetwork.CreateRoom(roomName.ToString(),roomOptions,TypedLobby.Default,null);//take above room options
    }//If this function is executed because of failure of Onclick_PlayNow()
    //Creating room with random number and join it.


    #region ButtonClicks
    public void OnClick_CreateBtn()
    {
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers=4;
        PhotonNetwork.CreateRoom(createRoom.text,roomOptions,TypedLobby.Default,null);//take above room options
    }
    public void OnClick_JoinBtn()
    {
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.MaxPlayers=4;
        PhotonNetwork.JoinOrCreateRoom(joinRoom.text,roomOptions,TypedLobby.Default,null);
    }
    public void OnClick_PlayNow()
    {
        PhotonNetwork.JoinRandomRoom();
        statusText.text="Creating Room...Please Wait...";
        //Joining Random room if it is available
        //if it is not available and failed to connect, OnJoinRandomFailed function will be executed
    }//function for Button PlayNow

    #endregion


}

 

Build Setting에서 꼭 로비랑, 본 게임 씬을 설정해줘야 한다.

나는 본 게임 씬을 인덱스1에 설정해둬서 PhotonNetwork.LoadLevel(1) 이다.

 

 

실행결과

 

 

반응형