VioletaBabel
56. Photon Tutorial (2) 본문
PlayButton이란 UI 버튼을 하나 만들고, Child인 Text를 Play로 바꿔 넣어준다.
그리고 PlayButton의 Onclick 필드에 Hierarchy의 Launcher를 드래그해 넣어주고, Connect()를 실행시키게 한다.
그 후 Launcher.cs의 Start() 부분을 제거한다.
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 | //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()을 사용. //모든 클라이언트는 갱신을 받거나 참여했을 때 새로운 씬을 로드. } 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(null, new RoomOptions() { maxPlayers = MaxPlayersPerRoom }, null); } public override void OnJoinedRoom() { Debug.Log("DemoAnimator/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room."); } } | cs |
PlayerNameInputField.cs를 만든다.
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 | //PlayerNameInputField.cs using System.Collections; using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(InputField))] // InputField 컴포넌트를 자동으로 추가해준다. public class PlayerNameInputField : MonoBehaviour { static string playerNamePrefKey = "PlayerName"; // PlayerPrefs 키는 static 추천. 게임 내에서 변치 않고 언제나 동일. private void Start() { string defaultName = ""; InputField _inputField = this.GetComponent<InputField>(); if (_inputField != null) if (PlayerPrefs.HasKey(playerNamePrefKey)) // PlayerPrefs는 게임 세션 사이에 플레이어의 preference 저장, 접근. { // HasKey는 인수로 들어간 키가 존재하는지 확인 후 bool로 리턴. defaultName = PlayerPrefs.GetString(playerNamePrefKey); // 키에 대응하는 값을 스트링으로 가져옴. _inputField.text = defaultName; } PhotonNetwork.playerName = defaultName; //룸에 입장한 모든 플레이어들과 플레이어의 별명을 동기화하기 위해 설정. //PhotonPlayer.name이 설정 됨. 별명과 같은 것으로 유일하지 않아도 됨. //별명은 계정을 대체하지 않는다. 플레이어 이름을 접근하는 것은 PhotonPlayer.name //원격 player가 설정한 playerName을 가진 타 player 목록은 PhotonNetwork.otherPlayers. } public void SetPlayerName(string value) // InputField의 OnValueChange()에서 이 함수를 호출해 바인드하면 편집 때마다 저장. { // 또는 Play를 눌렀을 때 호출할 수도 있음. 어쨌든 그런 식으로 쓴다고 함. PhotonNetwork.playerName = value + " "; PlayerPrefs.SetString(playerNamePrefKey, value); // key와 value를 playerprefs에 저장. } } | cs |
그 다음 UI의 InputField를 만들고, PlayerNameInputField.cs를 넣어준다.
적절히 위치시킨 후 InputField 컴포넌트의 OnValueChanged (String) 필드를 +하여 추가했던 PlayerNameInputField.cs를 넣어주고 Dynamic String의 SetPlayerName을 선택한다.
실행하여 아무 문구나 적고 Play를 누른 뒤 정지했다가 다시 실행해보자. 입력했던 문구가 저장됨을 볼 수 있다.
UI의 Panel을 추가하고 이름을 Control Panel로 바꾼 후 Image 컴포넌트와 Canvas Renderer 컴포넌트를 Remove하자.
다음 Play Button과 name InputField를 자식으로 드래그해 넣는다.
UI의 Text를 생성 후 Progress Label로 이름 짓자.
Text Component의 Alignment를 가운데로 다 바꾼 후, Text의 Text를 Connecting...으로 바꿔준다.
그 후 Launcher.cs를 수정해주자.
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 57 58 59 60 61 62 63 | //Launcher.cs using UnityEngine; public class Launcher : Photon.PunBehaviour { //이 클라이언트의 게임 버전. 사용자는 게임 버전으로 구분됨. string _gameVersion = "1"; public PhotonLogLevel Loglevel = PhotonLogLevel.Informational; // 밑에서 한 것과 다르게, 이렇게 하면 인스펙터에 노출. public byte MaxPlayersPerRoom = 4; // 윗 줄처럼 얘도 이렇게 처리. public GameObject controlPanel; public GameObject progressLabel; 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() { progressLabel.SetActive(false); controlPanel.SetActive(true); } public void Connect() { progressLabel.SetActive(true); controlPanel.SetActive(false); 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() { progressLabel.SetActive(false); controlPanel.SetActive(true); 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(null, new RoomOptions() { maxPlayers = MaxPlayersPerRoom }, null); } public override void OnJoinedRoom() { Debug.Log("DemoAnimator/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room."); } } | cs |
그 뒤에 Hierarchy의 launcher 오브젝트를 눌러 Launcher.cs 컴포넌트의 Control Panel과 Progress Label 인스턴스에 각각 해당 오브젝트를 드래그해 넣어준다.
'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 |
55. Photon Tutorial (1) (0) | 2018.08.06 |