- [Photon] 리팩터링 및 점수 기록2025년 06월 01일
- SiJun-Park
- 작성자
- 2025.06.01.:54
점수 기록을 위해서 Score라는 키값을 만드려고 하였는데, 이렇게 하면 너무 길어져서 어떻게 할까 고민을 많이 했습니다.
그러다가 이전 RPG Project를 하며 공부하였던 리팩터링을 해서 깔끔하게 작성하고자 하였습니다.
public class DataManager : MonoPunSingletonManager<DataManager> { public static void RemoveData(string key, int number) { if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(key, out object obj)) { var table = obj as PhotonHashTable; if (table != null && table.ContainsKey(number)) { table.Remove(number); PhotonNetwork.CurrentRoom.SetCustomProperties(new PhotonHashTable { [key] = table }); } } } public static void AddData(string key, int number, object value) { PhotonHashTable roomtable = PhotonNetwork.CurrentRoom.CustomProperties; PhotonHashTable table; if (PhotonNetwork.CurrentRoom.CustomProperties.ContainsKey(key)) { table = new PhotonHashTable(); PhotonHashTable btable = (PhotonHashTable)roomtable[key]; foreach (DictionaryEntry entry in btable) { table[entry.Key] = entry.Value; } } else table = new PhotonHashTable(); table[number] = value; PhotonNetwork.CurrentRoom.SetCustomProperties(new PhotonHashTable { [key] = table }); } public static object GetData(string key, int number) { if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(key, out object obj)) { var table = obj as PhotonHashTable; if (table != null && table.ContainsKey(number)) { return table[number]; } } return null; } public static void UpdateData(string key, int number, object value) { if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(key, out object obj)) { var table = obj as PhotonHashTable; if (table != null) { table[number] = value; PhotonNetwork.CurrentRoom.SetCustomProperties(new PhotonHashTable { [key] = table }); } } else // 키가 존재안할 경우 새롭게 만들어줌 { var newdata = new PhotonHashTable { [number] = value }; PhotonNetwork.CurrentRoom.SetCustomProperties(new PhotonHashTable { [key] = newdata }); } } }
그래서 위 코드처럼 리팩터링을 진행하였고, 데이터를 삭제, 추가, Get, Update를 할 수 있도록 구현을 하였습니다.
public override void OnPlayerEnteredRoom(Player newplayer) { if (!PhotonNetwork.IsMasterClient) return; AddData("PlayerState", newplayer.ActorNumber, State.Idle); AddData("Score", newplayer.ActorNumber, 0); } public override void OnPlayerLeftRoom(Player otherPlayer) { if (PhotonNetwork.IsMasterClient) { RemoveData("PlayerState", otherPlayer.ActorNumber); RemoveData("Score", otherPlayer.ActorNumber); } }
이전보다 더 깔끔하고 명확하게 알 수 있게 되었습니다.
public void Die(object obj, PlayerControlManager owner = null) { Dead = true; switch (obj) { case PlayerControlManager player: player.ChangeState(State.Die); PlayerDead(); int score = player.TextManager.GetScore(); Debug.Log($"Player Score : {(int)score} "); UpdateData("Score", PhotonNetwork.LocalPlayer.ActorNumber, score); // Data 업데이트 break; ... }
이제 몬스터를 잡는다면 Score점수가 ++가 되도록 이전에 설정을 하였습니다.
그러면 몬스터를 처치시 지속적으로 업데이트를 해주면 너무 비효율적이니
플레이어가 죽게 된다면 이때까지 쌓아둔 스코어를 불러와서 업데이트를 한번 해주도록 하였습니다.
결과 'Unity > FPS Project' 카테고리의 다른 글
[Photon] 몬스터 공격 수정 (0) 2025.06.10 [Photon] 방 입장 및 레디 상태 추가 (0) 2025.06.07 [Photon] 몬스터 피해 및 소환 알고리즘 수정 (0) 2025.05.31 [Photon] 채팅 구현 (0) 2025.05.29 [Photon] 플레이어 사망 상태 추가 (0) 2025.05.26 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)