Unity/Photon

08. Photon - ์‹ค์Šต 1 (6) Photon Nickname

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

- ๋‹‰๋„ค์ž„ ์„ค์ •

 

๐Ÿ”ถ ๋‹‰๋„ค์ž„ ์„ค์ •

- UI ์„ธํŒ…

 

 

 

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

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

    public TMP_InputField userIdText;
    public TMP_InputField roomNameText;

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

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

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

    void Start()
    {
        Debug.Log("00. ํฌํ†ค ๋งค๋‹ˆ์ € ์‹œ์ž‘");
        userId = PlayerPrefs.GetString("USER_ID", $"USER_{Random.Range(0, 100):00}");
        userIdText.text = userId;
        PhotonNetwork.NickName = userId;
    }
    public override void OnConnectedToMaster()
    {
        Debug.Log("01. ํฌํ†ค ์„œ๋ฒ„์— ์ ‘์†");

        //๋กœ๋น„์— ์ ‘์†
        PhotonNetwork.JoinLobby();

    }

    public override void OnJoinedLobby()
    {
        Debug.Log("02. ๋กœ๋น„์— ์ ‘์†");
    }

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

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

        roomNameText.text = $"Room_{Random.Range(1, 100):000}";

        // ๋ฃธ์„ ์ƒ์„ฑ > ์ž๋™ ์ž…์žฅ๋จ
        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");
        }
    }

    #region UI_BUTTON_CALLBACK
    // Random ๋ฒ„ํŠผ ํด๋ฆญ
    public void OnRandomBtn()
    {
        // ID ์ธํ’‹ํ•„๋“œ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด
        if (string.IsNullOrEmpty(userIdText.text))
        {
            // ๋žœ๋ค ์•„์ด๋”” ๋ถ€์—ฌ
            userId = $"USER_{Random.Range(0, 100):00}";
            userIdText.text = userId;
        }

        PlayerPrefs.SetString("USER_ID", userIdText.text);
        PhotonNetwork.NickName = userIdText.text;
        PhotonNetwork.JoinRandomRoom();
    }

    #endregion

}

- userId = PlayerPrefs.GetString("USER_ID", $"USER_{Random.Range(0, 100):00}");

    - PlayerPrefs์—์„œ USER_ID ํ‚ค์™€ ๋Œ€์‘๋˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ด

    - USER_ID ํ‚ค๊ฐ€ ์—†์œผ๋ฉด USER_{Random.Range(0, 100):00} ๊ฐ’์„ return

 

 

 

- ์—ฐ๊ฒฐ

 

 

 

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

 

 

 

- play

- Nickname์— ๋žœ๋ค ๊ฐ’์ด ๋“ค์–ด๊ฐ

- Random ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Level_1์”ฌ์„ ๋ถˆ๋Ÿฌ์˜ด

 

 

 

- Player์˜ PhotonView๋ฅผ ๋ณด๋ฉด Nickname์ด ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

'Unity > Photon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Photon - VR(XR Toolkit), Voice  (0) 2023.06.14
09. Photon - ์‹ค์Šต 1 (7) Room List  (6) 2021.08.25
07. Photon - ์‹ค์Šต 1 (5) Photon Voice  (0) 2021.08.23
06. Photon - ์‹ค์Šต 1 (4) Lobby, Custom Property  (0) 2021.08.22
05. Photon - ์‹ค์Šต 1 (3) Score  (1) 2021.08.18