1. 程式人生 > >unity3d UGUI製作技能cd效果

unity3d UGUI製作技能cd效果

使用UGUI簡單實現技能cd效果

using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
public class TestUi : MonoBehaviour
{
    public float coldTime = 10;//技能的冷卻時間
    private float currentTime = 0;//當前冷卻時間

    public Text skillCDText;
    public Image Mask;
    public Button skillBtn;

    void Awake()
    {
        Mask.fillAmount = 0;
        skillCDText.text = null;
    }

    void Update()
    {
        SkillTimeCalculator();
    }

    public void UseSkill()
    {
        Debug.Log("使用技能");
        currentTime = coldTime;
        Mask.fillAmount = 1;
        skillCDText.text = coldTime.ToString();
        skillBtn.enabled = false;
    }

    private void SkillTimeCalculator()
    {
        if (currentTime <= 0)
        {
            Mask.fillAmount = 0;
            skillCDText.text = null;
            skillBtn.enabled = true;
            return;
        }
        else
        {
            currentTime -= Time.deltaTime;
            var value = currentTime / coldTime;
            Mask.fillAmount = value;
            skillCDText.text = ((int)currentTime + 1).ToString();
        }
    }

}