Unity/Photon

02. Photon ์—ฐ๊ฒฐ, ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ

๐Ÿ”ท Photon ์—ฐ๊ฒฐ

- ๋นˆ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ : PhotonManager

 

 

 

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

 

 

 

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

 

 

 

๐Ÿ”ถ PhotonManage.cs ์ž‘์„ฑ

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

public class PhotonManager : MonoBehaviourPunCallbacks
{
    private readonly string gameVersion = "v1.0";
    private string userId = "Ojui";

    private void Awake()
    {
        // ๊ฒŒ์ž„ ๋ฒ„์ „ ์ง€์ •
        PhotonNetwork.GameVersion = gameVersion;

        // ์„œ๋ฒ„ ์ ‘์†
        PhotonNetwork.ConnectUsingSettings();
    }

    void Start()
    {
        Debug.Log("00. ํฌํ†ค ๋งค๋‹ˆ์ € ์‹œ์ž‘");
        PhotonNetwork.NickName = userId;
    }
    public override void OnConnectedToMaster()
    {
        Debug.Log("01. ํฌํ†ค ์„œ๋ฒ„์— ์ ‘์†");
        PhotonNetwork.JoinRandomRoom();

    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        Debug.Log("02. ๋žœ๋ค ๋ฃธ ์ ‘์† ์‹คํŒจ");

        // ๋ฃธ ์†์„ฑ ์„ค์ •
        RoomOptions ro = new RoomOptions();
        ro.IsOpen = true;
        ro.IsVisible = true;
        ro.MaxPlayers = 30;


        // ๋ฃธ์„ ์ƒ์„ฑ > ์ž๋™ ์ž…์žฅ๋จ
        PhotonNetwork.CreateRoom("room_1", ro);
    }

    public override void OnCreatedRoom()
    {
        Debug.Log("03. ๋ฐฉ ์ƒ์„ฑ ์™„๋ฃŒ");
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("04. ๋ฐฉ ์ž…์žฅ ์™„๋ฃŒ");
    }

}

 

๐Ÿ”ถ Photon ์—ฐ๊ฒฐ ํ™•์ธ

- ์œ ๋‹ˆํ‹ฐ play ํ›„ ์ฝ˜์†”์ฐฝ์— ์œ„์™€ ๊ฐ™์€ ๋กœ๊ทธ๊ฐ€ ๋œจ๋ฉด, ์—ฐ๊ฒฐ ์„ฑ๊ณตํ•œ ๊ฒƒ

 

 

 

๐Ÿ”ท ๋ฐฉ ์ž…์žฅํ•˜๋ฉด์„œ ์”ฌ ๋„˜์–ด๊ฐ€๊ธฐ

- ์”ฌ ์ƒ์„ฑ : PlayScene

 

 

 

- ๊ตฌ๋ถ„ํ•˜๊ธฐ ์‰ฝ๋„๋ก ์„ธํŒ…

- cube ์ƒ์„ฑ : Floor

    - Scale : 10, 0.1, 10

 

 

 

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

public class PhotonManager : MonoBehaviourPunCallbacks
{
    private readonly string gameVersion = "v1.0";
    private string userId = "Ojui";

    private void Awake()
    {
        // ๋ฐฉ์žฅ์ด ํ˜ผ์ž ์”ฌ์„ ๋กœ๋”ฉํ•˜๋ฉด, ๋‚˜๋จธ์ง€ ์‚ฌ๋žŒ๋“ค์€ ์ž๋™์œผ๋กœ ์‹ฑํฌ๊ฐ€ ๋จ
        PhotonNetwork.AutomaticallySyncScene = true;

        // ๊ฒŒ์ž„ ๋ฒ„์ „ ์ง€์ •
        PhotonNetwork.GameVersion = gameVersion;

        // ์„œ๋ฒ„ ์ ‘์†
        PhotonNetwork.ConnectUsingSettings();
    }

    void Start()
    {
        Debug.Log("00. ํฌํ†ค ๋งค๋‹ˆ์ € ์‹œ์ž‘");
        PhotonNetwork.NickName = userId;
    }
    public override void OnConnectedToMaster()
    {
        Debug.Log("01. ํฌํ†ค ์„œ๋ฒ„์— ์ ‘์†");
        PhotonNetwork.JoinRandomRoom();

    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        Debug.Log("02. ๋žœ๋ค ๋ฃธ ์ ‘์† ์‹คํŒจ");

        // ๋ฃธ ์†์„ฑ ์„ค์ •
        RoomOptions ro = new RoomOptions();
        ro.IsOpen = true;
        ro.IsVisible = true;
        ro.MaxPlayers = 30;


        // ๋ฃธ์„ ์ƒ์„ฑ > ์ž๋™ ์ž…์žฅ๋จ
        PhotonNetwork.CreateRoom("room_1", ro);
    }

    public override void OnCreatedRoom()
    {
        Debug.Log("03. ๋ฐฉ ์ƒ์„ฑ ์™„๋ฃŒ");
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("04. ๋ฐฉ ์ž…์žฅ ์™„๋ฃŒ");
        if (PhotonNetwork.IsMasterClient)
        {
            PhotonNetwork.LoadLevel("PlayScene");
        }
    }

}

- PhotonNetwork.LoadLevel("์”ฌ์ด๋ฆ„");

 

 

 

- Ctrl + Shift + B : Build Settings ์—ด๊ธฐ

- ์”ฌ์„ ์ˆœ์„œ์— ๋งž๊ฒŒ Drag&Drop

 

 

 

- SampleScene์œผ๋กœ ๋Œ์•„์™€์„œ playํ•ด๋ณด๋ฉด, PlayScene์œผ๋กœ ์ „ํ™˜๋จ

- ์”ฌ์ „ํ™˜๋˜๋ฉด์„œ ์„ค์ •ํ•ด๋‘” ๋จธํ‹ฐ๋ฆฌ์–ผ ์ƒ‰์ƒ๋ณด๋‹ค ์–ด๋‘์›Œ๋ณด์ธ๋‹ค๋ฉด ๋ผ์ดํŠธ๋งต์„ ๊ตฌ์›Œ์ฃผ๋ฉด ํ•ด๊ฒฐ๋จ