C#/수업내용

2020.08.05. 수업내용 - NGUI(2)

dev_sr 2020. 8. 5. 13:07

 

UIShop을 구성해줌

스크롤뷰를 만들어서 아이템 항목들을 보여줄거임

NGUI에서는 Scrollview에 grid가 붙음

NGUI 메뉴에서 스크롤뷰를 만들어줌

 

 

빈오브젝트로 grid를 만들어주고

Grid 컴포넌트를 꼭 붙여줘야함

Cell Width, Cell Height는 항목 사이의 거리를 조절해줌

Hide inactive 는 Grid밑에 inactive된 자식들이 있는 경우 얘네를 무시하게 해줌

체크 안하면 비활성화된 항목들이 자리를 차지하게됨 (걔네 자리를 남겨놓고 그만큼 뒤에 생김)

 

 

스크롤 뷰에 들어갈 UIListItem 을 만들어줌

실행되면서 4개가 스크롤 뷰에 만들어짐

 

 

 

 

전체 코드

 

UITitle

 

UITitle에서 재화버튼을 각각 누른 것마다 다른 icon이 나오도록 enumType으로 설정함

Open으로 enumType을 넘겨주고 오브젝트를 생성할 때 넘겨받은 enumType별로 각각 다른 icon들로

나타나도록 설정함 

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


public enum eMenuType
{
    Items,
    Shop,
    Message,
    Mission,
    Ranking,
    Setting,

}

public enum eBudgetType
{
    SoulGem,
    Coin,
    Gem
}


public class UITitle : MonoBehaviour
{
    public UIButton btnStart;
    public UIButton[] arrBtnMenus;
    public UIButton btnFriend;
    public UIButton btnFacebook;

    public UIBudgets uiBudgets;

    public UIShop uiShop;

    void Start()
    {
        //this.uiShop.Init();
        this.uiShop.btnClose.onClick.Add(new EventDelegate(() =>
        {
            this.uiShop.Close();
        }));
        this.uiBudgets.Init();

        this.btnStart.onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("start");
        }));    

        for(int i=0; i<this.arrBtnMenus.Length; i++)
        {
            var btn = this.arrBtnMenus[i];
            var idx = i;
            btn.onClick.Add(new EventDelegate(() =>
            {
                var menuType = (eMenuType)idx;
                Debug.Log(menuType);
            }));
        }

        this.btnFriend.onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("friend");
        }));

        this.btnFacebook.onClick.Add(new EventDelegate(() =>
        {
            Debug.Log("facebook");
        }));

        this.uiBudgets.btnSoulGem.onClick.Add(new EventDelegate(() => { 
            Debug.Log("소울잼 상점 열기");
            this.uiShop.Open(eBudgetType.SoulGem);
        }));
        this.uiBudgets.btnGem.onClick.Add(new EventDelegate(() => { 
            Debug.Log("잼 상점 열기");
            this.uiShop.Open(eBudgetType.Gem);
        }));
        this.uiBudgets.btnCoin.onClick.Add(new EventDelegate(() => { 
            Debug.Log("코인 상점 열기");
            this.uiShop.Open(eBudgetType.Coin);
        }));

        

    }



    
}

 

 

UIBudget

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

public class UIBudgets : MonoBehaviour
{

    public UILabel soulgemLabel;
    public UILabel coinLabel;
    public UILabel gemLabel;


    public UIButton btnSoulGem;
    public UIButton btnCoin;
    public UIButton btnGem;


    public void Init()
    {
        this.soulgemLabel.text = "FULL";
        this.coinLabel.text = "1000000";
        this.gemLabel.text = "300";
    }
}

 

 

UIShop

 

열릴 때 Init을 하고 항목들을 생성함

닫을 때 grid 밑에 달린 자식들을 찾아와서 gameobject를 찾아서 지워줌

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;

public class UIShop : MonoBehaviour
{
    public UIGrid grid;
    public GameObject uiListItemPrefab;
    public UIButton btnClose;
    private string[] arrAmount = { "5", "500", "10,000", "100,000" };
    private eBudgetType eBudgetType;

    void Start()
    {
        
    }

    public void Init()
    {


        for(int i=0; i<4; i++)
        {
            var go = Instantiate(this.uiListItemPrefab);
            var listItem = go.GetComponent<UIListItem>();

            var strAmounts = this.arrAmount[i];

            string iconName = null;


            //json 이 있을 때 id찾기
            var id = i + 100;
           
            //이미지 변경
            if(eBudgetType==eBudgetType.SoulGem)
            {
                iconName = string.Format("shop_img_soulgem_0{0}", i + 1);
                Debug.Log(iconName);
            }
            else if (eBudgetType == eBudgetType.Coin)
            {
                iconName = string.Format("shop_img_coin_0{0}", i + 1);
                Debug.Log(iconName);

            }
            else if (eBudgetType == eBudgetType.Gem)
            {
                iconName = string.Format("shop_img_gem_0{0}", i + 1);
                Debug.Log(iconName);

            }

            listItem.Init(id, iconName,strAmounts);
            listItem.btn.onClick.Add(new EventDelegate(() =>
            {
                Debug.Log(id);
            }));

            go.transform.SetParent(this.grid.transform);
            go.transform.localScale = Vector3.one;

        }

        grid.Reposition();  //리스트 아이템 정렬

    }

    public void Open(eBudgetType type)
    {
        this.gameObject.SetActive(true);
        this.eBudgetType = type;
        this.Init();
    }

    public void Close()
    {
        var list = this.grid.GetChildList();
        for(int i=0; i<4; i++)
        {
            Destroy(list[i].gameObject);
        }
        
        this.gameObject.SetActive(false);
    }
}

 

 

UIListItem

 

NGUI에서는 Sprite 타입 변수의 spriteName 속성에 sprite이름을 넣으면 sprite가 바뀜

using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using UnityEngine;

public class UIListItem : MonoBehaviour
{
    public UISprite icon;
    public UILabel lable;
    public int id;
    public UIButton btn;

    public void Init(int id,string iconName, string label)
    {

        this.id = id;
        this.icon.spriteName = iconName;
        this.lable.text = label;

        Debug.LogFormat("UIListItem : {0}",this.icon.spriteName);
    }

}