VioletaBabel

55. Photon Tutorial (1) 본문

BCA/8. Photon
55. Photon Tutorial (1)
Beabletoet 2018. 8. 6. 12:50

포톤 튜토리얼


Launcher라는 게임 오브젝트를 만들고 아래 스크립트를 자식으로 넣어준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Launcher.cs
using UnityEngine;
 
public class Launcher : Photon.PunBehaviour
{
    //이 클라이언트의 게임 버전. 사용자는 게임 버전으로 구분됨.
    string _gameVersion = "1";
    public PhotonLogLevel Loglevel = PhotonLogLevel.Informational; // 밑에서 한 것과 다르게, 이렇게 하면 인스펙터에 노출.
    public byte MaxPlayersPerRoom = 4// 윗 줄처럼 얘도 이렇게 처리.
    private void Awake()
    {
        //PhotonNetwork.logLevel = PhotonLogLevel.Full;   //네트워크의 로그 레벨을 최대로 올림.
                                                          //예상하는대로 동작한다는 확신이 서면 
                                                          //PhotonLogLevel.Informational로 바꾸는 게 좋음.
        PhotonNetwork.logLevel = Loglevel;      //Loglevel을 위 public에서 받아오게 하자. 인스펙터에서 건들 수 있다.
        PhotonNetwork.autoJoinLobby = false;    //마스터 서버에 접속 시 로비에 참여해야 하는지 정의.
                                                //false면 마스터에 접속 시 OnConnectedToMaster()가 호출되고 
                                                //OnJoinedLobby()는 호출되지 않음. 디폴트 값은 true.
        PhotonNetwork.automaticallySyncScene = true;    //마스터 클라이언트와 같은 레벨로 룸의 모든 클라이언트를 로드하는지.
                                    //로드된 레벨을 동기화 해야하면 마스터 클라이언트는 PhotonNetwork.LoadLevel()을 사용.
                                    //모든 클라이언트는 갱신을 받거나 참여했을 때 새로운 씬을 로드.
    }
    private void Start()
    {
        Connect();
    }
    public void Connect()
    {
        if (PhotonNetwork.connected)
            PhotonNetwork.JoinRandomRoom();//현재 사용 중인 로비에서 사용 가능한 룸에 참여, 룸이 없으면 실패.
        else
            PhotonNetwork.ConnectUsingSettings(_gameVersion);   //에디터에서 설정된 Photon에 연결. 오프라인 사용 불가.
                                                                //유효한 AppID가 설정 파일 내에 있어야 함.
                                                                //게임이 네트워크를 통하여 포톤클라우드로 연결되는 시작 지점.
    }
 
    public override void OnConnectedToMaster()
    {
        Debug.Log("DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN");
        PhotonNetwork.JoinRandomRoom();
    }
    public override void OnDisconnectedFromPhoton()
    {
        Debug.Log("DemoAnimator/Launcher: OnDisconnectedFromPhoton() was called by PUN");
    }
    public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
    {
        Debug.Log("DemoAnimator/Launcher: OnPhotonRandomJoinFailed() was called by PUN. No random room available, " +
            "so we create one.\nCalling: PhotonNetwork.CreateRoom(null, new RoomOption() {maxPlayers = 4},null)");
        PhotonNetwork.CreateRoom(nullnew RoomOptions() { maxPlayers = MaxPlayersPerRoom }, null);
    }
    public override void OnJoinedRoom()
    {
        Debug.Log("DemoAnimator/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
    }
}
cs


'BCA > 8. Photon' 카테고리의 다른 글

60. Photon Tutorial (6)  (0) 2018.08.06
59. Photon Tutorial (5)  (0) 2018.08.06
58. Photon Tutorial (4)  (0) 2018.08.06
57. Photon Tutorial (3)  (0) 2018.08.06
56. Photon Tutorial (2)  (0) 2018.08.06
Comments