본문 바로가기

유니티/수업 내용

[UGUI] 버젯, InputField

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIBudgets : MonoBehaviour
{
    public UIBudgetItem[] uiBudgetItems;

    void Start()
    {
        Init(100, 10, 10, 5);
    }

    public void Init(int gold, int energy, int maxEnergy, int gem)
    {
        this.uiBudgetItems[0].Init(gold);
        this.uiBudgetItems[1].Init(energy, maxEnergy);
        this.uiBudgetItems[2].Init(gem);
    }

    public void UpdateGold(int gold)
    {
        this.uiBudgetItems[0].text.text = gold.ToString();
    }

    public void UpdateEnergy(int energy)
    {
        this.uiBudgetItems[1].text.text = energy.ToString();
    }

    public void UpdateGem(int gem)
    {
        this.uiBudgetItems[2].text.text = gem.ToString();
    }
}
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UIBudgetItem : MonoBehaviour
{
    public enum eBudgetItemType
    {
        None = -1, Gold, Energy, Gem
    }

    private Button btn;
    public eBudgetItemType type;
    public TMP_Text text;

    void Start()
    {
        this.btn = this.GetComponent<Button>();
        this.btn.onClick.AddListener(() =>
        {
            Debug.Log("click");
        });
    }

    public void Init(int val)
    {
        this.text.text = val.ToString();
    }

    public void Init(int val, int maxVal)
    {
        this.text.text = val.ToString() + " /" + maxVal.ToString();
    }
}

에너지의 text를 두개로 안나누고 한개로만 나타내봤다

 


InputField

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class UIEventReciver : MonoBehaviour, IPointerClickHandler
{
    public UnityEvent<PointerEventData> OnPointerClickEvent;

    public void OnPointerClick(PointerEventData eventData)
    {
        OnPointerClickEvent.Invoke(eventData);
    }
}
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIInputField : MonoBehaviour
{
    public UIEventReciver uiEventReceiver;
    public GameObject placeHolderGo;
    private Coroutine routine;
    private float blickTime = 0.8f;

    void Start()
    {
        this.routine = StartCoroutine(this.EffPlaceHolderRoutine());
        this.uiEventReceiver.OnPointerClickEvent.AddListener((evtData) =>
        {
            Debug.LogFormat("click: {0}", evtData);
            this.placeHolderGo.SetActive(false);
            if (this.routine != null)
            {
                StopCoroutine(this.routine);
            }
        });
    }

    private IEnumerator EffPlaceHolderRoutine()
    {
        while (true)
        {
            this.placeHolderGo.SetActive(true);
            yield return new WaitForSeconds(this.blickTime);
            this.placeHolderGo.SetActive(false);
            yield return new WaitForSeconds(this.blickTime);
        }
    }
}

'유니티 > 수업 내용' 카테고리의 다른 글

[UGUI] Joystick  (0) 2022.01.18
[UGUI] UIStage  (0) 2022.01.18
[UGUI] 스킬버튼, 아이템버튼  (0) 2022.01.18
[UGUI] UI만들기 - Tab  (0) 2022.01.18
[UGUI] UI 만들기  (0) 2022.01.18