스토리지

[4.21] 게임데이터를 바탕으로 스테이지 씬 만들기 본문

Unity/유니티 기본

[4.21] 게임데이터를 바탕으로 스테이지 씬 만들기

ljw4104 2021. 4. 21. 11:55

결과물, 양쪽 화살표를 누를때마다 페이지가 이동되고 밑의 내비게이션이 바뀐다. / 유저의 StageInfo. 못깬 스테이지는 아예 기록을 하지 않는다.

 

  • 모든 스테이지 버튼은 동적으로 생성된다.
  • 왼쪽 또는 오른쪽으로 이동할때마다 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);
                }
            }
        }
    }
}
Comments