일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- dynamic
- Tutorial
- Basic
- Unity
- Class
- Algorithm
- 오류
- guide
- UE5
- dfs
- loop
- W3Schools
- Programming
- String
- 파이썬
- python
- c++
- Unreal Engine 5
- parameter
- DP
- 문제풀이
- 백준
- 기초
- 프로그래밍
- github
- 재귀
- Material
- 시작해요 언리얼 2022
- C#
- w3school
- Today
- Total
행복한 개구리
Unity 팀프 21.05.21 PUN2 - 스크립트 정리 및 Scene전환 본문
Unity 팀프 21.05.21 PUN2 - 스크립트 정리 및 Scene전환
HappyFrog 2021. 5. 22. 00:16using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
void Start()
{
SceneManager.LoadScene("Logo");
DontDestroyOnLoad(this);
}
void Update()
{
}
}
앱이 실행되자마자 Logo씬으로 넘어가도록 했다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Logo : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
yield return new WaitForSeconds(2.5f);
SceneManager.LoadScene("Title");
}
// Update is called once per frame
void Update()
{
}
}
로고씬에서는 로고를 띄울 시간을 코루틴으로 재생한 뒤 Title씬으로 넘어갔다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Photon.Realtime;
public class Title : MonoBehaviourPunCallbacks
{
public Button connect;
public InputField nickName;
#region PhotonNetwork Settings
[SerializeField]
private int gameVersion;
#endregion
private void Awake()
{
PhotonNetwork.AutomaticallySyncScene = true;
}
void Start()
{
Init();
}
void Init()
{
connect.onClick.AddListener(() =>
{
if (!string.IsNullOrEmpty(nickName.text))
{
ConnectToServer();
}
else
{
Debug.Log("Type your NickName");
}
});
}
void ConnectToServer()
{
PhotonNetwork.NickName = nickName.text;
PhotonNetwork.GameVersion = gameVersion.ToString();
PhotonNetwork.ConnectUsingSettings();
}
void Update()
{
}
#region PunCallbacks
[PunRPC]
public override void OnConnectedToMaster()
{
Debug.Log("Connected To Master");
PhotonNetwork.JoinLobby();
SceneManager.LoadScene("Lobby");
}
#endregion
}
Title씬에서는 인풋필드의 내용이 접속하는 닉네임이 되며, 인풋필드 내의 값이 없다면 접속할 수 없도록 했다.
그리고 해당 세팅으로 접속하며 로비로 참가하게 했으며 이어서 바로 Lobby씬을 불러왔다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
public class Lobby : MonoBehaviourPunCallbacks
{
public UILobby uiLobby;
public UIPopupRoomList uiPopupRoomList;
public UIPopupProfile uiPopupProfile;
public GameObject roomItemPrefab;
public Transform roomListGrid;
Dictionary<string, RoomInfo> cachedRoomList;
Dictionary<string, GameObject> roomItemEntries;
private void Start()
{
cachedRoomList = new Dictionary<string, RoomInfo>();
roomItemEntries = new Dictionary<string, GameObject>();
this.uiLobby.btnStart.onClick.AddListener(() =>
{
SceneManager.LoadScene("InGame");
});
Init();
}
void Init()
{
this.uiLobby.btnProfile.onClick.AddListener(() =>
{
uiPopupProfile.gameObject.SetActive(true);
uiPopupProfile.btnClose.onClick.AddListener(() =>
{
uiPopupProfile.gameObject.SetActive(false);
});
});
this.uiLobby.btnRoomList.onClick.AddListener(() =>
{
uiPopupRoomList.gameObject.SetActive(true);
});
}
#region RoomListUpdate
void UpdateCachedRoomList(List<RoomInfo> roomList)
{
foreach (var room in roomList)
{
if (room.RemovedFromList)
{
if (cachedRoomList.ContainsKey(room.Name))
{
cachedRoomList.Remove(room.Name);
}
}
else if (cachedRoomList.ContainsKey(room.Name))
{
cachedRoomList[room.Name] = room;
}
else
{
cachedRoomList.Add(room.Name, room);
}
}
}
void UpdateRoomListView()
{
foreach (var room in cachedRoomList.Values)
{
var entry = Instantiate(this.roomItemPrefab, this.roomListGrid);
var uiRoom = entry.GetComponent<UIRoomListItem>();
uiRoom.Init(room);
roomItemEntries.Add(room.Name, entry);
}
}
void ClearRoomListView()
{
foreach (var entry in roomItemEntries.Values)
{
Destroy(entry.gameObject);
}
roomItemEntries.Clear();
}
#endregion
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
ClearRoomListView();
UpdateCachedRoomList(roomList);
UpdateRoomListView();
Debug.Log("OnRoomListUpdate");
}
public override void OnJoinedLobby()
{
cachedRoomList.Clear();
ClearRoomListView();
Debug.Log("Welcome. This is Lobby :D");
}
public override void OnLeftLobby()
{
cachedRoomList.Clear();
ClearRoomListView();
Debug.Log("LeftLobby");
}
public override void OnJoinedRoom()
{
cachedRoomList.Clear();
Debug.Log("Joined Room");
}
public override void OnLeftRoom()
{
Debug.Log("Left Room");
UpdateRoomListView();
}
public override void OnCreatedRoom()
{
Debug.Log("Created Room");
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Debug.Log("Player Entered");
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
Debug.Log("Player Left");
}
public override void OnConnectedToMaster()
{
Debug.Log("Back To Lobby");
PhotonNetwork.JoinLobby();
}
}
;ㅏㅜ4ㅣ;2ㅏ4231ㅜ4;3로비씬에서는 UI처리와 서버적으로 처리할 것이있어서 Lobby라는 오브젝트에서는 UI의 팝업관리와 서버적인 처리를 했으며, 그 하위계층에서의 팝업관리는 UI스크립트들에서 했다.
1.이전에 연습했던 내용과 2개의 딕셔너리를 먼저 만든 다음 방정보은 Lobby에 있을때 자동으로 업데이트 해주는OnRoomListUpdate를 foreach문을 통하여 cachedRoomList에 저장했으며
2.cachedRoomList를 이용하여 만든 UI게임오브젝트들은 생성하는 동시에 roomItemEntries로 저장했다.
3.그리고 방에서 나갈때와 들어올때, 로비에 들어오고 나갈때 ClearRoomListView를 통해서 화면에 존재하는 UI게임오브젝트들을 초기화시켰다.
**주의 LeaveRoom을 하면 자동적으로 OnConnectedToMaster콜백메서드가 실행된다. 이를 구현하지 않는다면 LeaveRoom을 한 뒤에는 클라이언트가 Room에도, Lobby에도 속하지 않게된다. 반드시 구현할 것.
ㄴ OnLeftRoom의 내용으로 JoinLobby를 추가해봤지만, 소용없었다.
OnConnectedToMaster의 내용에 로비참가를 구현할 것.
-----------------------------------------------------------------------------------------------------------------------------------
Build And Run을 사용해보았다. 동시에 유니티실행을 두개의 창으로 하니 혼자서 테스트를 할 수 있어서 상당히 편했다. 그 와중에 불편한 점은 PC는 안드로이드가 아니여서 안드로이드 빌드로 제작중인 프로젝트가 Build And Run이 되지 않는다는 것이었다. 그래서 해당 프로그램을 업데이트 해줄때는 유니티환경을 PC로 바꾼 뒤, Build And Run하여 업데이트 시키고 작업하는 유니티창은 다시 안드로이드로 빌드해주었다.
'Unity > Project : Cursed Treasure' 카테고리의 다른 글
Unity 팀프 21.05.22. PUN2 - 동기화 (0) | 2021.05.22 |
---|---|
Unity 팀프 21.05.19~20 PUN - 채팅 스크롤뷰 (0) | 2021.05.22 |
Unity PUN2 21.05.18. JoinRoom 구현, 채팅 구현 (0) | 2021.05.18 |
Unity 팀프로젝트 21.05.14. Pun2 - 방목록 출력 (0) | 2021.05.14 |
Unity 팀프 21.05.13. PUN서버구축 (0) | 2021.05.13 |