Photon

[Photon] Player Settings, 포스트 프로세싱, 포톤 프로젝트 설정

다모아 2023. 11. 24. 14:04

https://github.com/IndieGameMaker/UnityBook

 

GitHub - IndieGameMaker/UnityBook: 절대강좌! 유니티 2021 - 리소스

절대강좌! 유니티 2021 - 리소스. Contribute to IndieGameMaker/UnityBook development by creating an account on GitHub.

github.com

이거 다운로드 오래걸림 1시간

 

https://assetstore.unity.com/packages/tools/network/pun-2-free-119922

 

PUN 2 - FREE | 네트워크 | Unity Asset Store

Get the PUN 2 - FREE package from Photon Engine and speed up your game development process. Find this & other 네트워크 options on the Unity Asset Store.

assetstore.unity.com

에셋 임포트

 

Setup Project하기

 

PhotonManager sciprt 만들

 

using JetBrains.Annotations;
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class PhotonManager : MonoBehaviourPunCallbacks
{
    private readonly string version = "1.0";
    private string userId = "lcyds110";

    public Button btnJoinLobby;
    public Button btnLeaveLobby;

    public GameObject roomCreateGo;
    public Button createRoomBtn;
    public TMP_InputField roomNameInputField;
    public Button btnLeaveRoom;
    public GameObject playerImageGo;
    public TMP_Text roomName;
    public TMP_Text roomPlayerCount;
    public Button btnRandomJoinRoom;

    private void Awake()
    {
        //마스터 클라이언트 (방장)
        PhotonNetwork.AutomaticallySyncScene = true; // 룸에있는 사람들을 데리고 자동으로 씬 전환
        PhotonNetwork.GameVersion = version;
        PhotonNetwork.NickName = userId;
        //위에까지가 세팅
        Debug.Log(PhotonNetwork.SendRate);
        PhotonNetwork.ConnectUsingSettings();
   
    }

    private void Start()
    {
        Debug.Log("Start");
        this.btnJoinLobby.gameObject.SetActive(false);
        this.btnLeaveLobby.gameObject.SetActive(false);
        this.playerImageGo.SetActive(false);
        this.btnRandomJoinRoom.gameObject.SetActive(false);

        this.btnJoinLobby.onClick.AddListener(() => {
            Debug.Log("join");
            PhotonNetwork.JoinLobby();
        });

        this.btnLeaveLobby.onClick.AddListener(() => {
            Debug.Log("leave");
            PhotonNetwork.LeaveLobby();
        });

        this.roomCreateGo.SetActive(false);

        this.createRoomBtn.onClick.AddListener(() => {
            string roomName = roomNameInputField.text;
            if(string.IsNullOrEmpty(roomName))
            {
                Debug.LogFormat("<color=red>방 이름을 입력해야합니다.</color>");
            }
            else
            {
                Debug.Log("방 입장 완료");
                this.CreateRoom(roomName);
            }
        });

        this.btnLeaveRoom.onClick.AddListener(() => {
            Debug.Log("leave room");
            PhotonNetwork.LeaveRoom();
            this.playerImageGo.SetActive(false);
        });

        this.btnRandomJoinRoom.onClick.AddListener(() => {
            Debug.Log("Random Join");
            PhotonNetwork.JoinRandomRoom();
        });
    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("마스터 서버에 접속");
        Debug.LogFormat("PhotonNetwork.InLobby: {0}", PhotonNetwork.InLobby);
        this.btnJoinLobby.gameObject.SetActive(true);
    }

    public override void OnJoinedLobby()
    {
        Debug.Log("로비 접속");
        Debug.LogFormat("PhotonNetwork.InLobby = {0}", PhotonNetwork.InLobby);

        this.roomCreateGo.SetActive(true);
        this.btnJoinLobby.gameObject.SetActive(false);
        this.btnLeaveLobby.gameObject.SetActive(true);
        this.btnRandomJoinRoom.gameObject.SetActive(true);
        //만들어져있는 읨의 룸에 입장을 시도, 룸이 없다면 Falled
        //PhotonNetwork.JoinRandomRoom();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        Debug.LogFormat("{0}, {1}", returnCode, message);
        //this.CreateRoom("my room");
    }

    public void CreateRoom(string roomName)
    {
        RoomOptions options = new RoomOptions();
        options.MaxPlayers = 20;
        options.IsOpen = true;
        options.IsVisible = true;

        PhotonNetwork.CreateRoom(roomName, options);
    }

    //룸 생성 성공
    public override void OnCreatedRoom()
    {
        Debug.Log("룸 생성완료");
        Debug.LogFormat("PhotonNetwork.CurrentRoom.Name: {0}", PhotonNetwork.CurrentRoom.Name);
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("룸 접속");
        this.roomCreateGo.SetActive(false);
        this.btnLeaveLobby.gameObject.SetActive(false);
        this.btnRandomJoinRoom.gameObject.SetActive(false);
        Debug.LogFormat("PhotonNetwork.CurrentRoom.PlayerCount : {0}", PhotonNetwork.CurrentRoom.PlayerCount);

        foreach(var pair in PhotonNetwork.CurrentRoom.Players)
        {
            var player = pair.Value;
            Debug.LogFormat("pair.Key: {0}", pair.Key);
            Debug.LogFormat("player.NickName: {0}, player.ActorNumber: {1}", player.NickName, player.ActorNumber);
        }

        this.roomName.text = roomNameInputField.text;
        this.roomPlayerCount.text = PhotonNetwork.CurrentRoom.PlayerCount + "명";
        this.playerImageGo.SetActive(true);
    }

    //룸 생성 실패
    public override void OnCreateRoomFailed(short returnCode, string message)
    {
        Debug.LogFormat("{0}, {1}", returnCode, message);
    }

    public override void OnLeftLobby()
    {
        Debug.Log("로비에서 나갔습니다.");

        this.btnLeaveLobby.gameObject.SetActive(false);
        this.btnJoinLobby.gameObject.SetActive(true);
        this.roomCreateGo.SetActive(false);
    }

    public override void OnLeftRoom()
    {
        Debug.Log("룸에서 나갔습니다.");
    }
}

 


포톤 서버 접속하는법

App Id PUN을 대상자의 아이디로 변경하면 그 대상자의 서버로 접속할 수 있다.

 

ui 구