64 lines
1.2 KiB
C#
64 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class CoolDown : MonoBehaviour, IPointerClickHandler, IEventSystemHandler
|
|
{
|
|
public Image overlay;
|
|
|
|
private bool isCoolDown;
|
|
|
|
private float coolDownDuration;
|
|
|
|
private float coolDownInitTime;
|
|
|
|
public bool IsCoolDown
|
|
{
|
|
get
|
|
{
|
|
return isCoolDown;
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (overlay != null)
|
|
{
|
|
if (Time.time - coolDownInitTime < coolDownDuration)
|
|
{
|
|
overlay.fillAmount = Mathf.Clamp01(1f - (Time.time - coolDownInitTime) / coolDownDuration);
|
|
}
|
|
else
|
|
{
|
|
overlay.fillAmount = 0f;
|
|
}
|
|
isCoolDown = overlay.fillAmount > 0f;
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
Cooldown(3f, 1.5f);
|
|
}
|
|
|
|
public void Cooldown(float coolDown, float globalCoolDown)
|
|
{
|
|
if (!isCoolDown)
|
|
{
|
|
coolDownDuration = coolDown;
|
|
coolDownInitTime = Time.time;
|
|
isCoolDown = true;
|
|
base.transform.root.BroadcastMessage("GlobalCooldown", globalCoolDown, SendMessageOptions.DontRequireReceiver);
|
|
}
|
|
}
|
|
|
|
private void GlobalCooldown(float coolDown)
|
|
{
|
|
if (Time.time + coolDownInitTime * coolDownDuration < Time.time + coolDownInitTime * coolDown || !isCoolDown)
|
|
{
|
|
coolDownDuration = coolDown;
|
|
coolDownInitTime = Time.time;
|
|
}
|
|
}
|
|
}
|