_xiaofang/xiaofang/Assets/Script/UI/SkillCoolDownUI.cs
2024-10-24 12:46:20 +08:00

47 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class SkillCoolDownUI : MonoBehaviour
{
public Image cooldownImage; // 拖动Image组件到这里
public Text cooldowntext;//显示计时器剩余时间
public event Action OnCooldownFinished; // 冷却结束事件
private Coroutine countdownCoroutine;
public void StartCooldown(float cooldownTime)
{
if (countdownCoroutine != null)
{
StopCoroutine(countdownCoroutine);
}
countdownCoroutine = StartCoroutine(CooldownRoutine(cooldownTime));
}
private IEnumerator CooldownRoutine(float cooldownTime)
{
float elapsed = 0f;
cooldownImage.fillAmount = 1; // 设置为满
while (elapsed < cooldownTime)
{
elapsed += Time.deltaTime;
cooldownImage.fillAmount = 1 - (elapsed / cooldownTime);
cooldowntext.text =((1 - (elapsed / cooldownTime))*10).ToString("F1");
yield return null;
}
HandleCooldownFinished();
}
private void HandleCooldownFinished()
{
cooldownImage.fillAmount = 0; // 重新设置为0
OnCooldownFinished?.Invoke(); // 触发冷却结束事件
Destroy(gameObject);
}
}