Unity/Photon

06. Photon - ์‹ค์Šต 1 (4) Lobby, Custom Property

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

- ๋กœ๋น„์—์„œ ํ”Œ๋ ˆ์ด์–ด ์ƒ‰์ƒ์„ ์ •ํ•˜๊ณ  ๊ฒŒ์ž„์”ฌ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ

 

๐Ÿ”ถ Lobby

- Level_1 ์”ฌ์„ ๋ณต์‚ฌ

- ์ด๋ฆ„์„ Lobby๋กœ ๋ณ€๊ฒฝ

 

 

 

- Player ํ”„๋ฆฌํŒน ํ•˜์ด์–ด๋ผํ‚ค ์ฐฝ์œผ๋กœ ๋“œ๋ž˜๊ทธ&๋“œ๋กญ

 

 

 

- position, rotation

- ์ปดํฌ๋„ŒํŠธ 5๊ฐœ ์‚ญ์ œ

 

 

 

- player : Unpack Completely

 

 

 

- position

- Clear Flags : Solid Color

- Background

 

 

 

- UI ์ˆ˜์ •

 

 

 

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. ํฌํ†ค ์„œ๋ฒ„์— ์ ‘์†");

    }

    public void OnStartBtn()
    {
        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("Level_1");
        }
    }

}

 

 

 

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 LobbyManager : MonoBehaviour
{
    private PhotonView pv;

    private Hashtable CP;

    private Ray ray;
    private RaycastHit hit;
    private new Camera camera;

    public Material[] playerMt;

    public Renderer player;

    void Start()
    {
        pv = GetComponent<PhotonView>();
        camera = Camera.main;

        PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable { { "Color", -1 } });
        CP = PhotonNetwork.LocalPlayer.CustomProperties;
    }

    private void Update()
    {
        ray = camera.ScreenPointToRay(Input.mousePosition);

        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(ray, out hit, 100.0f, 1 << 6))
            {
                string item = hit.collider.gameObject.name;
                string[] words = item.Split('_');
                SetColorProperty(int.Parse(words[1]));
            }
        }
    }

    public void SetColorProperty(int num)
    {
        CP["Color"] = num;
        SetMt(num);
    }

    void SetMt(int num)
    {
        player.material = playerMt[num - 1];
    }

}

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

 

 

 

- GameManager ์‚ญ์ œ

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

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

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

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

- 6๋ฒˆ ๋ ˆ์ด์–ด ์ถ”๊ฐ€ : Item

- Item_1, _2, _3์— ๋ ˆ์ด์–ด ์„ค์ • : Item

 

 

 

- play

- ๊ณต ๋ˆ„๋ฅด๋ฉด ์ƒ‰์ƒ ๋ณ€ํ•˜๋Š” ๊ฒƒ ํ™•์ธ

 

 

 

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

 

 

 

- play

- Start ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด Level_1 ์”ฌ์œผ๋กœ ๋„˜์–ด๊ฐ

 

 

 

๐Ÿ”ถ Level_1

- PhotonManager ์‚ญ์ œ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityStandardAssets.Utility;
using UnityEngine.SceneManagement;

public class PlayerCtrl : MonoBehaviourPunCallbacks
{
    private new Rigidbody rigidbody;
    private PhotonView pv;

    private float v;
    private float h;
    private float r;

    [Header("์ด๋™ ๋ฐ ํšŒ์ „ ์†๋„")]
    public float moveSpeed = 8.0f;
    public float turnSpeed = 200.0f;
    public float jumpPower = 5.0f;
    private float turnSpeedValue = 0.0f;

    private RaycastHit hit;

    public Material[] playerMt;
    private int idxMt = -1;

    private void Awake()
    {
        rigidbody = GetComponent<Rigidbody>();
        pv = GetComponent<PhotonView>();
    }

    IEnumerator Start()
    {

        turnSpeedValue = turnSpeed;
        turnSpeed = 0.0f;
        yield return new WaitForSeconds(0.5f);

        if (pv.IsMine)
        {
            Camera.main.GetComponent<SmoothFollow>().target = transform.Find("CamPivot").transform;
        }
        else
        {
            GetComponent<Rigidbody>().isKinematic = true;
        }


        turnSpeed = turnSpeedValue;
    }

    // ํ‚ค ์ž…๋ ฅ
    void Update()
    {
        v = Input.GetAxis("Vertical");
        h = Input.GetAxis("Horizontal");
        r = Input.GetAxis("Mouse X");

        Debug.DrawRay(transform.position, -transform.up * 0.6f, Color.green);
        if (Input.GetKeyDown("space"))
        {
            if (Physics.Raycast(transform.position, -transform.up, out hit, 0.6f))
            {
                rigidbody.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
            }
        }

    }

    // ๋ฌผ๋ฆฌ์  ์ฒ˜๋ฆฌ
    void FixedUpdate()
    {
        if (pv.IsMine)
        {
            Vector3 dir = (Vector3.forward * v) + (Vector3.right * h);
            transform.Translate(dir.normalized * Time.deltaTime * moveSpeed, Space.Self);
            transform.Rotate(Vector3.up * Time.smoothDeltaTime * turnSpeed * r);
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        string coll = other.gameObject.name;
        switch (coll)
        {
            case "Item_1":
                ScorePoint(0);
                break;
            case "Item_2":
                ScorePoint(1);
                break;
            case "Item_3":
                ScorePoint(2);
                break;
        }

    }

    public void InitColor(int num)
    {
        pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, num);
    }

    [PunRPC]
    public void SetMt(int idx)
    {
        GetComponent<Renderer>().material = playerMt[idx];
        idxMt = idx;
    }

    public void ScorePoint(int idx)
    {
        pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, idx);
        GameManager.instance.GetScore(pv.ViewID / 1000);
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        if (pv.IsMine && idxMt != -1)
        {
            pv.RPC(nameof(SetMt), newPlayer, idxMt);
        }
    }

}

 

 

 

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 GameManager : MonoBehaviour
{
    public static GameManager instance = null;

    public PhotonView pv;

    public Transform[] spawnPoints;

    public Text scorePlayer1Text;
    public Text scorePlayer2Text;

    private int scorePlayer1 = 0;
    private int scorePlayer2 = 0;

    private Hashtable CP;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else if (instance != this)
        {
            Destroy(this.gameObject);
        }
    }

    void Start()
    {
        pv = GetComponent<PhotonView>();
        CP = PhotonNetwork.LocalPlayer.CustomProperties;
        CreatePlayer();
    }

    void CreatePlayer()
    {
        spawnPoints = GameObject.Find("SpawnPointGroup").GetComponentsInChildren<Transform>();

        Vector3 pos = spawnPoints[PhotonNetwork.CurrentRoom.PlayerCount].position;
        Quaternion rot = spawnPoints[PhotonNetwork.CurrentRoom.PlayerCount].rotation;

        GameObject playerTemp = PhotonNetwork.Instantiate("Player", pos, rot, 0);

        int colorNum = (int)CP["Color"];
        Debug.Log($"colorNum : {colorNum}");
        if (colorNum != -1)
        {
            playerTemp.GetComponent<PlayerCtrl>().InitColor(colorNum - 1);
        }
    }

    public void GetScore(int playerNum)
    {
        pv.RPC(nameof(SetScore), RpcTarget.AllViaServer, playerNum);
    }

    [PunRPC]
    public void SetScore(int playerNum)
    {
        if (playerNum == 1)
        {
            scorePlayer1++;
            scorePlayer1Text.text = $"{scorePlayer1:00}";
        }
        else
        {
            scorePlayer2++;
            scorePlayer2Text.text = $"{scorePlayer2:00}";
        }
    }
}

 

 

 

- play

 

 

 

- ์„ ํƒํ•œ ์ƒ‰์ƒ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ