유니티3D 프로그래밍
Unity UGUI Test 2 (21.04.20~21) 본문
캐릭터 정보 및 인벤토리 구성
왼쪽에는 캐릭터 정보, 오른쪽에는 인벤토리를 구성하는 형식이다.
오른쪽 인벤토리는 스크롤을 구현하여 인벤토리를 구성 할 예정이다.
스크립트 구현 예정.
Stage 구현
세이브와 로드는 임의로 추가한 버튼으로 현재 스테이지 정보들을 Json을 통해 파일로 저장, 불러오기 기능을 추가했다.
위의 장면은 게임 시작전 장면이고 밑의 장면은 저장된 데이터가 없이 처음 시작하는 경우 보여주는 장면이다.
화면 상단 위쪽에 골드나 보석은 예시며 오른쪽 상단의 별 개수는 아직 연동이 되있지 않다. 첫번째 페이지에서는 왼쪽으로 갈 수 없으므로 왼쪽 가는 버튼이 비활성화 되어있다.
현재 Scene의 구성 오브젝트들이다.
Scene을 구성하는데 필요한 스크립트
GameInfo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameInfo
{
public List<StageInfo> stageInfos;
public GameInfo()
{
this.stageInfos = new List<StageInfo>();
}
}
HomeButton
아직 미구현. 버튼을 통해 로브로 돌아그는 역할 구현 예정
Lobby
데이터 파일을 불러와 미리 Load하는 역할
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class Lobby : MonoBehaviour
{
public static GameInfo gameInfo;
public StageUI uiStage;
void Start()
{
var path = string.Format("{0}/game_info.json", Application.persistentDataPath);
Debug.Log(path);
if (File.Exists(path))
{
var json = File.ReadAllText(path);
Debug.Log(json);
Lobby.gameInfo = JsonConvert.DeserializeObject<GameInfo>(json);
}
this.uiStage.Init();
}
}
SaveTest
Save와 Load의 기능 구현 테스트 스크립트
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class SaveTest : MonoBehaviour
{
public Button btnSave;
public Button btnLoad;
private GameInfo gameInfo;
private int maxStage = 36;
private void Start()
{
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);
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");
}
this.btnSave.onClick.AddListener(() => {
Debug.Log("save");
Debug.LogFormat("dataPath: {0}", Application.dataPath);
Debug.LogFormat("persistentDataPath: {0}", Application.persistentDataPath);
});
this.btnLoad.onClick.AddListener(() => {
Debug.Log("load");
});
}
}
StageButton
각각의 스테이지의 버튼 기능을 부여하는 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StageButton : MonoBehaviour
{
public Button btn;
}
StageInfo
플레이어의 스테이지 상태를 저장하는 스크립트, Json 파일을 이용함
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;
}
}
StageUI
입력한 스테이지의 정보를 변경, 페이지 이동, Lobby에서 각각 스테이지의 상태를 불러와 적용함
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StageUI : MonoBehaviour
{
public StageUIItem[] arrUIStageItems;
public Image[] pageCount;
public Image currentPageImage;
private int maxStage = 36;
private int currentPage = 1;
private int totalPage;
private int pageNum = 0;
public Button btnPrev;
public Button btnNext;
public void Init()
{
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);
for (int i = 0; i < this.arrUIStageItems.Length; i++)
{
var uiStageItem = this.arrUIStageItems[i];
uiStageItem.txtStageNo.text = (i + 1).ToString();
var info = Lobby.gameInfo.stageInfos[i];
uiStageItem.Init(info);
}
}
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++)
{
var uiStageItem = this.arrUIStageItems[i];
uiStageItem.txtStageNo.text = (startIndex + i + 1).ToString();
var idx = startIndex + i;
var info = Lobby.gameInfo.stageInfos[idx];
uiStageItem.Init(info);
}
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++)
{
var uiStageItem = this.arrUIStageItems[i];
uiStageItem.txtStageNo.text = (startIndex + i + 1).ToString();
var idx = startIndex + i;
var info = Lobby.gameInfo.stageInfos[idx];
uiStageItem.Init(info);
}
if (this.currentPage == this.totalPage)
{
this.btnNext.gameObject.SetActive(false);
}
else
{
this.btnNext.gameObject.SetActive(true);
}
this.btnPrev.gameObject.SetActive(true);
}
// Update is called once per frame
void Update()
{
}
}
StageUIITem
각각의 스테이지 상태(Clear, UnClear, Lock) 및 클리어 시 획득한 별 이미지 호출
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StageUIItem : MonoBehaviour
{
public GameObject ClearGo;
public GameObject UnClearGo;
public GameObject lockGo;
public GameObject[] arrStars;
public Text txtStageNo;
private StageInfo info;
public enum eStageType
{
CLEAR, UNCLEAR, LOCK
}
public eStageType type;
public void Init(StageInfo info)
{
this.info = info;
switch((eStageType)this.info.state)
{
case eStageType.CLEAR:
{
this.ClearGo.SetActive(true);
this.UnClearGo.SetActive(false);
this.lockGo.SetActive(false);
}
break;
case eStageType.UNCLEAR:
{
this.ClearGo.SetActive(false);
this.UnClearGo.SetActive(true);
this.lockGo.SetActive(false);
}
break;
case eStageType.LOCK:
{
this.ClearGo.SetActive(false);
this.UnClearGo.SetActive(false);
this.lockGo.SetActive(true);
}
break;
}
foreach (var star in this.arrStars)
{
star.gameObject.SetActive(false);
}
for (int i = 0; i < this.info.stars; i++)
{
this.arrStars[i].SetActive(true);
}
}
}
'Unity > 수업내용' 카테고리의 다른 글
Unity UGUI 상점 창 구현 (21.04.23) (0) | 2021.04.23 |
---|---|
Unity UGUI 미션 창 구현 (21.04.23) (0) | 2021.04.23 |
Unity UGUI Test (21.04.20) (0) | 2021.04.20 |
Unity 6주차 5일 수업 내용 : 다단계 Scene 구성 (21.04.06) (0) | 2021.04.16 |
Unity 6주차 3,4,5일 수업 내용 : Zombie (21.04.14) (0) | 2021.04.16 |