_xiaofang/xiaofang/Assets/Script/UI/SkillCoolDownUI.cs

58 lines
1.4 KiB
C#
Raw Normal View History

2024-10-24 12:46:20 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Threading.Tasks;
2024-10-24 12:46:20 +08:00
public class SkillCoolDownUI : MonoBehaviour
{
public Image cooldownImage; // <20>϶<EFBFBD>Image<67><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public Text cooldowntext;//<2F><>ʾ<EFBFBD><CABE>ʱ<EFBFBD><CAB1>ʣ<EFBFBD><CAA3>ʱ<EFBFBD><CAB1>
public event Action OnCooldownFinished; // <20><>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
private Coroutine countdownCoroutine;
public async void StartCooldown(float cooldownTime)
2024-10-24 12:46:20 +08:00
{
if (countdownCoroutine != null)
{
StopCoroutine(countdownCoroutine);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ
await CooldownRoutine(cooldownTime);
2024-10-24 12:46:20 +08:00
}
public async Task CooldownRoutine(float cooldownTime)
2024-10-24 12:46:20 +08:00
{
float elapsed = 0f;
cooldownImage.fillAmount = 1; // <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
while (elapsed < cooldownTime)
{
elapsed += Time.deltaTime;
cooldownImage.fillAmount = 1 - (elapsed / cooldownTime);
cooldowntext.text = ((1 - (elapsed / cooldownTime)) * 10).ToString("F1");
// ʹ<><CAB9> Task.Delay <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD>̵<EFBFBD> yield return
await Task.Yield(); // <20>ó<EFBFBD><C3B3><EFBFBD>ǰ֡<C7B0><D6A1><EFBFBD>ȴ<EFBFBD><C8B4>´θ<C2B4><CEB8><EFBFBD>
2024-10-24 12:46:20 +08:00
}
HandleCooldownFinished();
}
2024-10-24 12:46:20 +08:00
private void HandleCooldownFinished()
{
cooldownImage.fillAmount = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0
OnCooldownFinished?.Invoke(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
Destroy(gameObject);
}
2024-10-24 12:46:20 +08:00
}