๐ท ์ค์ต 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
- ์ ํํ ์์์ผ๋ก ์์ฑ๋๋ ๊ฒ์ ํ์ธ
'Unity > Photon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
08. Photon - ์ค์ต 1 (6) Photon Nickname (0) | 2021.08.24 |
---|---|
07. Photon - ์ค์ต 1 (5) Photon Voice (0) | 2021.08.23 |
05. Photon - ์ค์ต 1 (3) Score (1) | 2021.08.18 |
04. Photon - ์ค์ต 1 (2) ํ๋ ์ด์ด Material ๋ฐ๊พธ๊ธฐ (0) | 2021.08.17 |
03. Photon - ์ค์ต 1 (1) player ์์ฑ, ์์น ๋๊ธฐํ (0) | 2021.08.16 |