본문 바로가기
게임개발/게임 UI.UX 프로그래밍

스킬 버튼 만들기

by pudding81 2024. 2. 8.

 

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;


public class UISkillButton : MonoBehaviour
{
    public Button btn;
    private int progress; //슬라이더 채우는 시간
    public TMP_Text timeText;
    public int cooltime ; //쿨타임 입력
    public Image slider;
    private Coroutine coroutine;



    void Start()
    {
        this.timeText.text = string.Format("{0}",this.cooltime);
        btn.onClick.AddListener(() =>
        {
            Debug.Log("스킬사용");
            this.StartCoroutine(this.CoStartCooltime()); //코루틴 시작
                                                         
           
        });
        
        
    }

    IEnumerator CoStartCooltime()
    {
        while (true)
        {
            

            this.progress += 1;
              Debug.LogFormat("{0}", this.progress);
            this.UpdateProgressText();
            this.UpdateSlider();
            if (this.progress == this.cooltime)
            {
                //끝나면 다시 초기화 
                this .progress = 0;
                this.timeText.text = string.Format("{0}", this.cooltime);
                this.slider.fillAmount = 0;

                break;
            }

            yield return new WaitForSeconds(1f); //코루틴 돌아가는 시간 설정
           
        }

    }

    private void UpdateProgressText()
    {
        this.timeText.text = string.Format("{0}", this.progress); // 텍스트 입력
    }

    private void UpdateSlider()
    {
        this.slider.fillAmount = this.progress *(1f/this.cooltime);// 슬라이더 채우기 
    }

    
}

'게임개발 > 게임 UI.UX 프로그래밍' 카테고리의 다른 글

스크롤뷰 만들기  (0) 2024.02.08
버튼 눌러 상점 아이템 보여주기  (1) 2024.02.08
Input 필드 UI만들기  (0) 2024.02.07
로딩바 만들기  (0) 2024.02.07
스위치버튼, 탭 메뉴 UI 만들기  (0) 2024.02.06