KT Cloud x Photon PUN2 유니티 멀티게임 만들기 Webinar 1탄





NetworkManager.cs 소스입니다



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


public class NetworkManager : MonoBehaviourPunCallbacks
{
[Header("DisconnectPanel")]
public GameObject DisconnectPanel;
public InputField NicknameInput;

[Header("RoomPanel")]
public GameObject RoomPanel;
public Text ValueText, PlayersText, ClickUpgradeText, AutoUpgradeText,
ValuePerClickText, ValuePerSecondText;
public Button ClickUpgradeBtn, AutoUpgradeBtn;


float nextTime;


void Start()
    {
        Screen.SetResolution(540, 960, false);
    }

public void Connect()
{
PhotonNetwork.LocalPlayer.NickName = NicknameInput.text;
PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster()
{
        PhotonNetwork.JoinOrCreateRoom("Room", new RoomOptions { MaxPlayers = 5 }, null);
}

void ShowPanel(GameObject CurPanel)
{
DisconnectPanel.SetActive(false);
RoomPanel.SetActive(false);
CurPanel.SetActive(true);
}

public override void OnJoinedRoom()
{
ShowPanel(RoomPanel);
PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity);
}

PlayerScript FindPlayer() 
{
foreach (GameObject Player in GameObject.FindGameObjectsWithTag("Player")) 
if (Player.GetPhotonView().IsMine) return Player.GetComponent<PlayerScript>();
return null;
}

public void Click() 
{
PlayerScript Player = FindPlayer();
Player.value += Player.valuePerClick;
}

public void ClickUpgrade() 
{
PlayerScript Player = FindPlayer();

if (Player.value >= Player.clickUpgradeCost) 
{
Player.value -= Player.clickUpgradeCost;
Player.valuePerClick += Player.clickUpgradeAdd;
Player.clickUpgradeCost += Player.clickUpgradeAdd * 10;
Player.clickUpgradeAdd += 2;

ClickUpgradeText.text = "비용 : " + Player.clickUpgradeCost + "\n+" +
Player.clickUpgradeAdd + "/클릭";

ValuePerClickText.text = Player.valuePerClick.ToString();
}
}

public void AutoUpgrade()
{
PlayerScript Player = FindPlayer();

if (Player.value >= Player.autoUpgradeCost)
{
Player.value -= Player.autoUpgradeCost;
Player.valuePerSecond += Player.autoUpgradeAdd;
Player.autoUpgradeCost += 500;
Player.autoUpgradeAdd += 2;

AutoUpgradeText.text = "비용 : " + Player.autoUpgradeCost + "\n+" +
Player.autoUpgradeAdd + "/초";

ValuePerSecondText.text = Player.valuePerSecond.ToString();
}
}

void ShowPlayers() 
{
string playersText = "";
foreach (GameObject Player in GameObject.FindGameObjectsWithTag("Player")) 
{
playersText += Player.GetPhotonView().Owner.NickName + " / " +
Player.GetComponent<PlayerScript>().value.ToString() + "\n";
}
PlayersText.text = playersText;
}

void EnableUpgrade() 
{
PlayerScript Player = FindPlayer();
ClickUpgradeBtn.interactable = Player.value >= Player.clickUpgradeCost;
AutoUpgradeBtn.interactable = Player.value >= Player.autoUpgradeCost;
}

void ValuePerSecond() 
{
PlayerScript Player = FindPlayer();
Player.value += Player.valuePerSecond;
}

void Update()
{
if (!PhotonNetwork.InRoom) return;

ShowPlayers();
EnableUpgrade();

if (Time.time > nextTime)
{
nextTime = Time.time + 1;
ValuePerSecond();
}
}
}








PlayerScript.cs 소스입니다





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


public class PlayerScript : MonoBehaviourPun, IPunObservable
{
    public int value, valuePerClick, clickUpgradeCost, clickUpgradeAdd,
        valuePerSecond, autoUpgradeCost, autoUpgradeAdd;
    NetworkManager NM;
    PhotonView PV;

void Start()
    {
        PV = photonView;
        NM = GameObject.FindWithTag("NetworkManager").GetComponent<NetworkManager>();
    }

    void Update() 
    {
        if (!PV.IsMine) return;

        NM.ValueText.text = value.ToString();
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting) stream.SendNext(value);
        else value = (int)stream.ReceiveNext();
    }
}