Notice
Recent Posts
Recent Comments
Link
스토리지
[4.21] 게임데이터를 바탕으로 스테이지 씬 만들기 본문
- 모든 스테이지 버튼은 동적으로 생성된다.
- 왼쪽 또는 오른쪽으로 이동할때마다 18개씩 새로운 프리팹들이 생성된다. 이전의 프리팹은 삭제된다.
- 하단부의 Navigation은 미리 좌표를 지정해놓고 페이징이 될때마다 해당 좌표로 이동하는 형식이다.
- StageInfo 테이블에 데이터가 없다는 것은 Complete나 Doing상태가 아님을 뜻하므로 Locked상태로 놓았다.
StageInfo로부터 데이터를 받아오는 코드
DataManager.cs
public void GetStageDatas()
{
var data = Resources.Load("StageTable").ToString();
var arr = JsonConvert.DeserializeObject<StageInfo[]>(data);
this.stageArr = arr.ToDictionary(x => x.stageNo);
}
스테이지 버튼 하나하나를 초기화하는 코드
UIStageItem.cs
using UnityEngine;
using UnityEngine.UI;
public class UIStageItem : MonoBehaviour
{
public Button btn;
public Image sourceImage;
public Text stageNo;
public GameObject starPos;
public GameObject star;
public void Init(Sprite sprite, string stageNo, int starNum = 0)
{
this.sourceImage.sprite = sprite;
this.stageNo.text = stageNo;
for(int i = 0; i < starNum; i++)
{
Instantiate(this.star, this.starPos.transform);
}
}
}
UIStage.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.U2D;
public class UIStage : MonoBehaviour
{
public GameObject stagePrefab;
public Transform stagePos;
public SpriteAtlas atlas;
public Button btnHome;
public Button btnRight;
public Button btnLeft;
public RectTransform[] naviPos;
public Image navi;
public UIBudget[] budgets;
public Text txtStarNum;
private string lockSpriteName = "stage_frame_lock";
private string doingSpriteName = "stage_frame_default";
private string completeSpriteName = "stage_frame_complete";
private int pageNum;
public void Init()
{
this.pageNum = 1;
this.btnLeft.gameObject.SetActive(false);
this.txtStarNum.text = string.Format("{0}", 0);
this.btnHome.onClick.AddListener(() =>
{
SceneManager.LoadScene("Title");
});
this.btnRight.onClick.AddListener(() =>
{
this.EnterNextPage();
});
this.btnLeft.onClick.AddListener(() =>
{
this.EnterPreviousPage();
});
this.InitStageBtn();
}
private void EnterNextPage()
{
this.pageNum++;
if (this.pageNum > 1)
{
this.btnLeft.gameObject.SetActive(true);
}
this.InitStageBtn();
if (this.pageNum == this.naviPos.Length)
{
this.btnRight.gameObject.SetActive(false);
}
this.navi.rectTransform.position = new Vector2(naviPos[pageNum - 1].position.x, naviPos[pageNum - 1].position.y);
}
private void EnterPreviousPage()
{
this.pageNum--;
if (this.pageNum == 1)
{
this.btnLeft.gameObject.SetActive(false);
}
this.InitStageBtn();
if (this.pageNum <= this.naviPos.Length - 1)
{
this.btnRight.gameObject.SetActive(true);
}
this.navi.rectTransform.position = new Vector2(naviPos[pageNum - 1].position.x, naviPos[pageNum - 1].position.y);
}
private void InitStageBtn()
{
foreach(Transform child in this.stagePos)
{
Destroy(child.gameObject);
}
int x = (this.pageNum - 1) * 18 + 1;
for (int i = x; i < x + 18; i++)
{
var go = Instantiate(this.stagePrefab, this.stagePos);
var stage = go.GetComponent<UIStageItem>();
if (i > DataManager.Instance.stageArr.Count)
{
stage.Init(atlas.GetSprite(this.lockSpriteName), "");
stage.btn.enabled = false;
}
else
{
var stageData = DataManager.Instance.stageArr[i];
if (stageData.state == StageInfo.eState.Lock)
{
stage.Init(atlas.GetSprite(this.lockSpriteName), "");
stage.btn.enabled = false;
}
else if (stageData.state == StageInfo.eState.Doing)
{
stage.Init(atlas.GetSprite(this.doingSpriteName), stageData.stageNo.ToString());
}
else
{
stage.Init(atlas.GetSprite(this.completeSpriteName), stageData.stageNo.ToString(), stageData.starCount);
}
}
}
}
}
'Unity > 유니티 기본' 카테고리의 다른 글
[4.21] Json파일에서 읽은 미션을 스테이지에 적용 (0) | 2021.04.21 |
---|---|
[4.21] Persistent Data Path에 기반한 데이터 읽기/쓰기 (0) | 2021.04.21 |
[4.20] Atlas (0) | 2021.04.20 |
[4.20] 아이템 습득 후 인벤토리에 표시 (0) | 2021.04.20 |
[4.16] 유니티에서 Json파일 읽어오기 (0) | 2021.04.16 |
Comments