You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Unity.Multiplayer.Samples.BossRoom
{
public class AbilityUI : MonoBehaviour
{
public string key;
public Image CoolDownImg;
public GameObject CoolDownImgParent;
private Coroutine stopWatchRoutine;
private bool isCooldownActive = false;
public void StopWatchFiller(float coolDownTime)
{
if (stopWatchRoutine != null)
{
StopCoroutine(stopWatchRoutine);
}
stopWatchRoutine = StartCoroutine(StopWatchFillerRoutine(coolDownTime));
}
private IEnumerator StopWatchFillerRoutine(float coolDownTime)
{
isCooldownActive = true;
CoolDownImgParent.SetActive(true);
CoolDownImg.fillAmount = 1;
float elapsedTime = 0;
while (elapsedTime < coolDownTime)
{
// Check if cooldown is still active before updating UI
if (!isCooldownActive)
{
CoolDownImgParent.SetActive(false);
yield break;
}
elapsedTime += Time.deltaTime;
CoolDownImg.fillAmount = 1 - (elapsedTime / coolDownTime);
yield return null;
}
CoolDownImgParent.SetActive(false);
isCooldownActive = false;
stopWatchRoutine = null;
}
/// <summary>
/// Cancels cooldown UI immediately if the ability is available before the countdown ends.
/// </summary>
public void CancelCooldownUI()
{
if (stopWatchRoutine != null)
{
StopCoroutine(stopWatchRoutine);
stopWatchRoutine = null;
}
CoolDownImgParent.SetActive(false);
isCooldownActive = false;
}
}
}