Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

행복한 개구리

Unity GUI 21.04.21.수업내용 본문

Unity/수업내용

Unity GUI 21.04.21.수업내용

HappyFrog 2021. 4. 21. 18:28

이미지를 이렇게 뺄 때마다 중앙정렬을 시키려면 Horizon layer group컴포넌트를 이용하자.

============================================================================

UIStage의 계층구조는 이런식으로 잡았다. page는 UI아래쪽에 몇페이지인지 보여주는 불빛이 들어올 수 있게 이미지 하위에 empty를 만들어서 코드로 페이지를 넘길때마다 해당 페이지를 나타내는 칸에 불이 들어오게 했다.

그리고 contents는 한 페이지에 있는 스테이지 18개를 담고 있는데 grid는 그 중 한 줄을 나타낸다. contents또한 페이지가 넘어갈 때마다 해당되는 contents를 SetActive(true)시켜 페이지를 넘길 때마다 새로운 스테이지들이 나타나게 했다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Lobby : MonoBehaviour
{
    public UIStage stage;
    public UIStageLock stageLock;
    // Start is called before the first frame update
    void Start()
    {
        this.stage.itemPrefab.btn.onClick.AddListener(() =>
        {
            this.stageLock.gameObject.SetActive(true);
        });
    }

    // Update is called once per frame
    void Update()
    {

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIStageItem : MonoBehaviour
{
    public enum eStageItemType
    {
        Complete, Basic, Lock
    }

    public GameObject completeGo;
    public GameObject basicGo;
    public GameObject lockGo;
    public Button btn;
    public GameObject[] stars;
    public Text stageNum;
    private eStageItemType type;


    public void Init(eStageItemType type, int stars)
    {
        this.type = type;
        switch (this.type)
        {
            case (eStageItemType.Complete):
                {
                    this.completeGo.SetActive(true);
                    this.basicGo.SetActive(false);
                    this.lockGo.SetActive(false);
                    if (stars < 4)
                    {
                        for (int i = 0; i < stars; i++)
                        {
                            var star = this.stars[i];
                            star.SetActive(true);
                        }
                    }                    
                }
                break;
            case (eStageItemType.Basic):
                {
                    Debug.Log("Basic");
                    this.completeGo.SetActive(false);
                    this.basicGo.SetActive(true);
                    this.lockGo.SetActive(false);
                }
                break;
            case (eStageItemType.Lock):
                {
                    this.completeGo.SetActive(false);
                    this.basicGo.SetActive(false);
                    this.lockGo.SetActive(true);
                }
                break;
        }        
    }


    public void GetStar(int amount)
    {
        
    }

    // Start is called before the first frame update
    void Start()
    {
        this.btn.onClick.AddListener(() => 
        {
            Debug.Log("You clicked.");
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIStageLock : MonoBehaviour
{

    public Button close;
    public Button ok;
    public Text required;
    public Text stageNum;

    // Start is called before the first frame update
    void Start()
    {
        this.close.onClick.AddListener(() => this.gameObject.SetActive(false));

        this.ok.onClick.AddListener(() => this.gameObject.SetActive(false));
    }

    public void Init(int level,int stageNum)
    {
        this.required.text = "Required Level : " + level;
        this.stageNum.text = "Stage " + stageNum;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIStage : MonoBehaviour
{
    public Button btnHome;
    public Button btnPrev;
    public Button btnNext;
    public Text starCount;
    public Image[] pages = new Image[4];
    public List<UIStageItem> uiItems;
    public UIStageItem itemPrefab;
    public GameObject[] arrEmpty;
    public GameObject prefabLight;
    public GameObject[] page;
        
    GameObject light;
    private int stageNum = 0;
    private int totalStageNum = 4;

    private void Awake()
    {
        page[this.stageNum].gameObject.SetActive(true);
        for (int i = 0; i < this.uiItems.Count; i++)
        {
            var uiItem = this.uiItems[i];
            uiItem.stageNum.text = (this.uiItems.Count * (stageNum) + i + 1).ToString();
            uiItem.Init(UIStageItem.eStageItemType.Complete, 3);
            this.uiItems.Add(uiItem);
        }
        GameObject light = Instantiate(this.prefabLight, this.arrEmpty[this.stageNum].transform);
        this.light = light;
    }
    void Start()
    {        
        this.btnPrev.onClick.AddListener(() =>
        {
            Prev();
        });

        this.btnNext.onClick.AddListener(() =>
        {
            Next();
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void Prev()
    {   
        if (this.stageNum == 0) return;
        page[this.stageNum].gameObject.SetActive(false);
        this.stageNum--;
        page[this.stageNum].gameObject.SetActive(true);
        var startIndex = (this.stageNum - 1) * this.uiItems.Count;
        var endIndex = startIndex + this.uiItems.Count;

        GameObject newLight = Instantiate(this.prefabLight, this.arrEmpty[this.stageNum].transform);
        Destroy(this.light.gameObject);
        this.light = newLight;

        for (int i = 0; i < this.uiItems.Count; i++)
        {
            var uiItem = this.uiItems[this.uiItems.Count * stageNum + i];
            uiItem.stageNum.text = (this.uiItems.Count * (stageNum) + i + 1).ToString();            
        }
    }

    private void Next()
    {
        if (this.stageNum == 3) return;
        page[this.stageNum].gameObject.SetActive(false);
        this.stageNum++;
        page[this.stageNum].gameObject.SetActive(true);
        GameObject newLight = Instantiate(this.prefabLight, this.arrEmpty[this.stageNum].transform);
        Destroy(this.light.gameObject);
        this.light = newLight;

        for (int i = 0; i < this.uiItems.Count; i++)
        {
            var uiItem = this.uiItems[this.uiItems.Count * stageNum + i];
            uiItem.stageNum.text = (this.uiItems.Count * stageNum + i + 1).ToString();
            uiItem.Init(UIStageItem.eStageItemType.Basic, 0);
            this.uiItems.Add(uiItem);
        }

    }
}

-미구현 및 미비점

ㄴ1페이지 말곤 스테이지 제대로 작동안함

ㄴ스테이지 클릭 작동안함

ㄴ데이터매니저를 이용한 직렬화 구현x