CoolDown timer fixed and WarningPanel UI
parent
9633a7ae18
commit
a15f6d6b34
@ -1,46 +1,66 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Unity.Multiplayer.Samples.BossRoom
|
||||
{
|
||||
|
||||
public class AbilityUI : MonoBehaviour
|
||||
{
|
||||
public string key;
|
||||
public Image CoolDownImg;
|
||||
public GameObject CoolDownImgParent;
|
||||
public IEnumerator StopWatchRoutine;
|
||||
|
||||
private Coroutine stopWatchRoutine;
|
||||
private bool isCooldownActive = false;
|
||||
|
||||
public void StopWatchFiller(float coolDownTime)
|
||||
{
|
||||
if (StopWatchRoutine == null)
|
||||
{
|
||||
StopWatchRoutine = StopWatchFillerRoutine(coolDownTime);
|
||||
StartCoroutine(StopWatchRoutine);
|
||||
}
|
||||
else
|
||||
if (stopWatchRoutine != null)
|
||||
{
|
||||
StopCoroutine(StopWatchRoutine);
|
||||
StopWatchRoutine = StopWatchFillerRoutine(coolDownTime);
|
||||
StartCoroutine(StopWatchRoutine);
|
||||
StopCoroutine(stopWatchRoutine);
|
||||
}
|
||||
|
||||
stopWatchRoutine = StartCoroutine(StopWatchFillerRoutine(coolDownTime));
|
||||
}
|
||||
public IEnumerator StopWatchFillerRoutine(float coolDownTime)
|
||||
|
||||
private IEnumerator StopWatchFillerRoutine(float coolDownTime)
|
||||
{
|
||||
float factor = coolDownTime;
|
||||
isCooldownActive = true;
|
||||
CoolDownImgParent.SetActive(true);
|
||||
// CoolDownImg.gameObject.SetActive(true);
|
||||
CoolDownImg.fillAmount = 1;
|
||||
while (coolDownTime > 0)
|
||||
|
||||
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)
|
||||
{
|
||||
coolDownTime -= Time.deltaTime;
|
||||
CoolDownImg.fillAmount = coolDownTime / factor;
|
||||
yield return new WaitForSeconds(Time.deltaTime);
|
||||
StopCoroutine(stopWatchRoutine);
|
||||
stopWatchRoutine = null;
|
||||
}
|
||||
CoolDownImgParent.SetActive(false);
|
||||
// CoolDownImg.gameObject.SetActive(false);
|
||||
StopWatchRoutine = null;
|
||||
isCooldownActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public class WarningPanel : NetworkBehaviour
|
||||
{
|
||||
public static WarningPanel Instance { get; private set; }
|
||||
|
||||
[SerializeField] private GameObject panel;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowPanelForTime(float seconds)
|
||||
{
|
||||
if (IsServer) // Only the server should trigger this
|
||||
{
|
||||
ShowPanelClientRpc(seconds);
|
||||
}
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void ShowPanelClientRpc(float seconds)
|
||||
{
|
||||
panel.SetActive(true);
|
||||
StartCoroutine(HidePanelAfterTime(seconds));
|
||||
}
|
||||
|
||||
private IEnumerator HidePanelAfterTime(float seconds)
|
||||
{
|
||||
yield return new WaitForSeconds(seconds);
|
||||
panel.SetActive(false);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 725a3a3076d96f54f8ffcb254140c646
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue