-
04/21 UI json데이터를 읽고 스테이지 정보 출력유니티 2021. 4. 21. 14:42
Json데이터
더보기{
"stageInfos": [
{
"stageNo": 1,
"state": 0,
"stars": 1
},
{
"stageNo": 2,
"state": 0,
"stars": 2
},
{
"stageNo": 3,
"state": 1,
"stars": 0
},
{
"stageNo": 4,
"state": 2,
"stars": 0
},
{
"stageNo": 5,
"state": 2,
"stars": 0
},
{
"stageNo": 6,
"state": 2,
"stars": 0
},
{
"stageNo": 7,
"state": 2,
"stars": 0
},
{
"stageNo": 8,
"state": 2,
"stars": 0
},
{
"stageNo": 9,
"state": 2,
"stars": 0
},
{
"stageNo": 10,
"state": 2,
"stars": 0
},
{
"stageNo": 11,
"state": 2,
"stars": 0
},
{
"stageNo": 12,
"state": 2,
"stars": 0
},
{
"stageNo": 13,
"state": 2,
"stars": 0
},
{
"stageNo": 14,
"state": 2,
"stars": 0
},
{
"stageNo": 15,
"state": 2,
"stars": 0
},
{
"stageNo": 16,
"state": 2,
"stars": 0
},
{
"stageNo": 17,
"state": 2,
"stars": 0
},
{
"stageNo": 18,
"state": 2,
"stars": 0
},
{
"stageNo": 19,
"state": 2,
"stars": 0
},
{
"stageNo": 20,
"state": 0,
"stars": 2
},
{
"stageNo": 21,
"state": 2,
"stars": 0
},
{
"stageNo": 22,
"state": 2,
"stars": 0
},
{
"stageNo": 23,
"state": 2,
"stars": 0
},
{
"stageNo": 24,
"state": 2,
"stars": 0
},
{
"stageNo": 25,
"state": 2,
"stars": 0
},
{
"stageNo": 26,
"state": 2,
"stars": 0
},
{
"stageNo": 27,
"state": 2,
"stars": 0
},
{
"stageNo": 28,
"state": 2,
"stars": 0
},
{
"stageNo": 29,
"state": 2,
"stars": 0
},
{
"stageNo": 30,
"state": 2,
"stars": 0
},
{
"stageNo": 31,
"state": 2,
"stars": 0
},
{
"stageNo": 32,
"state": 2,
"stars": 0
},
{
"stageNo": 33,
"state": 2,
"stars": 0
},
{
"stageNo": 34,
"state": 2,
"stars": 0
},
{
"stageNo": 35,
"state": 2,
"stars": 0
},
{
"stageNo": 36,
"state": 2,
"stars": 0
}
]
}코드
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StageInfo { public int stageNo; public int state; public int stars; public StageInfo(int stageNo, int state = 2, int stars = 0) { this.stageNo = stageNo; this.state = state; this.stars = stars; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameInfo { public List<StageInfo> stageInfos; public int stars; public int Stars { get { stageInfos.ForEach((num) => { stars += num.stars; }); return stars; } } public GameInfo() { this.stageInfos = new List<StageInfo>(); } }
using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; public class UIStage : MonoBehaviour { public UIStageItem[] arrUIStageItems; private int maxStage = 36; private int currentPage = 1; private int totalPage; public Button btnPrev; public Button btnNext; private GameInfo gameInfo; public Text txtStars; private void Start() { this.totalPage = this.maxStage / this.arrUIStageItems.Length; this.btnPrev.onClick.AddListener(() => { this.PrevPage(); }); this.btnNext.onClick.AddListener(() => { this.NextPage(); }); if (this.currentPage == 1) { this.btnPrev.gameObject.SetActive(false); } else { this.btnPrev.gameObject.SetActive(true); } this.btnNext.gameObject.SetActive(true); var path = string.Format("{0}/game_info.json", Application.persistentDataPath); if (File.Exists(path)) { //기존 유저 var json = File.ReadAllText(path); Debug.Log(json); this.gameInfo = JsonConvert.DeserializeObject<GameInfo>(json); this.txtStars.text = gameInfo.Stars.ToString(); Debug.Log("loaded success"); } else { //신규 유저 //초기화 this.gameInfo = new GameInfo(); for (int i = 0; i < this.maxStage; i++) { var state = 2; if (i == 0) { state = 1; } var stageInfo = new StageInfo(i + 1, state); this.gameInfo.stageInfos.Add(stageInfo); } var json = JsonConvert.SerializeObject(this.gameInfo); Debug.Log(json); File.WriteAllText(path, json); Debug.Log("saved success"); } for(int i = 0; i < this.arrUIStageItems.Length; i++) { var uiStageItem = this.arrUIStageItems[i]; uiStageItem.Init((UIStageItem.eStageItemType)gameInfo.stageInfos[i].state, gameInfo.stageInfos[i].stageNo, gameInfo.stageInfos[i].stars); } //for (int i = 0; i < this.arrUIStageItems.Length; i++) //{ // var uiStageItem = this.arrUIStageItems[i]; // uiStageItem.txtStageNo.text = (i + 1).ToString(); // uiStageItem.Init(UIStageItem.eStageItemType.DOING); // //if (i == 0) // //{ // // uiStageItem.Init(UIStageItem.eStageItemType.DOING); // //} // //else { // // uiStageItem.Init(UIStageItem.eStageItemType.LOCK); // //} //} } private void PrevPage() { if (this.currentPage == 1) { return; } this.currentPage--; Debug.LogFormat("currentPage: {0}, totalPage: {1}", this.currentPage, this.totalPage); var startIndex = (this.currentPage - 1) * this.arrUIStageItems.Length; var endIndex = startIndex + this.arrUIStageItems.Length; Debug.LogFormat("{0} ~ {1}", startIndex, endIndex); for (int i = 0; i < this.arrUIStageItems.Length; i++) { this.arrUIStageItems[i].Init((UIStageItem.eStageItemType)gameInfo.stageInfos[startIndex + i].state, gameInfo.stageInfos[startIndex + i].stageNo, gameInfo.stageInfos[startIndex + i].stars); } //for (int i = 0; i < this.arrUIStageItems.Length; i++) //{ // var uiStageItem = this.arrUIStageItems[i]; // uiStageItem.txtStageNo.text = (startIndex + i + 1).ToString(); //} if (this.currentPage == 1) { this.btnPrev.gameObject.SetActive(false); } else { this.btnPrev.gameObject.SetActive(true); } this.btnNext.gameObject.SetActive(true); } private void NextPage() { if (this.currentPage == this.totalPage) { return; } this.currentPage++; Debug.LogFormat("currentPage: {0}, totalPage: {1}", this.currentPage, this.totalPage); var startIndex = (this.currentPage - 1) * this.arrUIStageItems.Length; var endIndex = startIndex + this.arrUIStageItems.Length; Debug.LogFormat("{0} ~ {1}", startIndex, endIndex); for (int i = 0; i < this.arrUIStageItems.Length; i++) { this.arrUIStageItems[i].Init((UIStageItem.eStageItemType)gameInfo.stageInfos[startIndex + i].state, gameInfo.stageInfos[startIndex + i].stageNo, gameInfo.stageInfos[startIndex + i].stars); } //for (int i = 0; i < this.arrUIStageItems.Length; i++) //{ // var uiStageItem = this.arrUIStageItems[i]; // uiStageItem.txtStageNo.text = (startIndex + i + 1).ToString(); //} if (this.currentPage == this.totalPage) { this.btnNext.gameObject.SetActive(false); } else { this.btnNext.gameObject.SetActive(true); } this.btnPrev.gameObject.SetActive(true); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIStageItem : MonoBehaviour { public enum eStageItemType { COMPLETE, DOING, LOCK } public GameObject completeGo; public GameObject doingGo; public GameObject lockGo; public GameObject[] arrStars; public Text txtStageNo; private eStageItemType type; private int stageNo; private int stars; public void Init(eStageItemType type, int stageNo, int stars) { this.type = type; this.stageNo = stageNo; this.txtStageNo.text = stageNo.ToString(); this.stars = stars; switch(this.type) { case eStageItemType.COMPLETE: { this.completeGo.SetActive(true); this.doingGo.SetActive(false); this.lockGo.SetActive(false); for (int i = stars; i < arrStars.Length; i++) { arrStars[i].SetActive(false); } } break; case eStageItemType.DOING: { this.completeGo.SetActive(false); this.doingGo.SetActive(true); this.lockGo.SetActive(false); } break; case eStageItemType.LOCK: { this.completeGo.SetActive(false); this.doingGo.SetActive(false); this.lockGo.SetActive(true); } break; } } }
'유니티' 카테고리의 다른 글
04/22 유니티 UIAnimation (0) 2021.04.22 04/22 UIAnimation R&D (0) 2021.04.22 04/16 수업내용 메모 (0) 2021.04.16 04/15 유니티 애니메이션 컨트롤러 연습 (0) 2021.04.16 04/15 유니티 LineRenderer 사용 연습 (0) 2021.04.15