자료실

[Photon엔진강좌] 초보자도 쉽게 할 수 있는 포톤 유니티 네트워크 (PUN) 튜토리얼 2 기초편
작성자 | admin 2020-08-31  |    조회수 : 2161  


* 그림은 클릭하면 크게 보실 수 있습니다. ^^

자 다시 시작해 볼까요?


메뉴 줄에서 GameObject 메뉴를 찾아주세요. GameObject 메뉴를 클릭하신 후 하단에 Create Empty 메뉴를 클릭합니다.





그러면 Hierarchy 에 GameObject 가 생성된 것이 잘 보입니다.

https://blog.naver.com/PostList.nhn?from=postList&blogId=jbaeg&categoryNo=0¤tPage=3#"

GameObject 를 더블 클릭하여 이름을 communicator 로 바꿔줍니다.
정보를 전달하는 통신기 역할을 할 것입니다.

https://blog.naver.com/PostList.nhn?from=postList&blogId=jbaeg&categoryNo=0¤tPage=3#" 그 다음 communicator에 붙일 Script 파일을 만들어 줍니다.
Project 에서 Scripts > Puzzle 에서 우측클릭을 한 후 Create > C# Script 를 클릭해 주세요.
https://blog.naver.com/PostList.nhn?from=postList&blogId=jbaeg&categoryNo=0¤tPage=3#" 파일 이름은 PlayerPhoton 으로 지어주세요.

https://blog.naver.com/PostList.nhn?from=postList&blogId=jbaeg&categoryNo=0¤tPage=3#" PlayerPhoton 스크립트 파일을 더블 클릭하여 MonoDeveloper 화면으로 넘어갑니다.

https://blog.naver.com/PostList.nhn?from=postList&blogId=jbaeg&categoryNo=0¤tPage=3#" 그 다음 PlayerPhoton 스크립트 파일에 다음과 같이 코드를 넣어줍니다.

Crtl +C , Ctrl + V
//----------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;

public class PlayerPhoton : Photon.PunBehaviour, IPunObservable {

public int iHp = 10; // 유저의 HP

public GameManager gameManager; // GameManager 객체

void Awake () {

DontDestroyOnLoad(gameObject);

gameManager = GameObject.Find("GameManager").transform.GetComponent ();
}

[PunRPC] // RPC 함수를 통하여 정보를 전달합니다.

public void AttackInfo(int attackTarget,int damage){

// attackTarget 공격할 대상 damage 적에게 주는 데미지

gameManager.AttackProcess (attackTarget,damage);
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// 우리 캐릭터의 정보를 다른 유저들에게 전송합니다. ^^
stream.SendNext(this.iHp);
}
else
{
// 다른 유저가 정보를 받습니다. ^^
this.iHp = (int)stream.ReceiveNext();
}
}
}
//------------------------------------------------------------------------------------



자 소스를 살펴 볼까요?

맨 윗줄에
public class PlayerPhoton : MonoBehaviour 를

public class PlayerPhoton : Photon.PunBehaviour, IPunObservable

이렇게 바꿔줬습니다.
이제 PUN의 기능을 쓸 수 있겠죠~

그다음 RPC함수를 만들어 줬습니다.
[PunRPC] // RPC 함수를 통하여 정보를 전달합니다.
public void AttackInfo(int attackTarget,int damage){
// attackTarget 공격할 대상 damage 적에게 주는 데미지
gameManager.AttackProcess (attackTarget,damage);
}

GameManager 에서 적을 공격할 때 적에게 damage를 전달하는 함수입니다.

그 다음 나의 정보가 바뀌면 그 정보를 다른 모든 유저들에게 전송하는 함수입니다.
RPC 함수를 통해 나의 정보가 바뀌면 그 정보를 다른 모든 유저들에게 전송하게 됩니다.
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// 우리 캐릭터의 정보를 다른 모든 유저들에게 전송합니다.
stream.SendNext(this.iHp);
}
else
{
// 다른 유저가 정보를 받습니다. ^^
this.iHp = (int)stream.ReceiveNext();
}
}
}

지금은 HP의 int형만 전달하는데 bool, string 형 모두 전송이 가능합니다. string 정보를 전달하면 게임상에서 채팅도 가능하겠죠!
이제 포톤을 통하여 정보를 전송할 준비가 다 되었습니다.


그럼 다음편에서는 다시 유니티로 돌아가서 계속 진행해 보겠습니다.


★더 많은 글은Photon HelpCenter
https://support.photonengine.jp/hc/ko/categories/204651467 에서 확인하세요!
★Photon 공식 홈페이지
https://www.photonengine.com/ko-kr/Photon