pudding81 2024. 2. 8. 11:05

 

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);// 슬라이더 채우기 
    }

    
}