我想包括一个冷却功能,我的技能按钮在我的手机游戏,我目前正在开发。所以我想让玩家每隔几秒钟只能使用一次技能按钮。
左键是我的默认技能键,右键是一个复制键。默认技能按钮将被放置在副本上,因此当我运行游戏时,单击默认技能按钮时,副本将与默认技能按钮重叠。

然而,在我的例子中,副本不能覆盖默认技能按钮,所以它不会显示冷却计时器。
我想知道我是否需要包括一组代码,以允许默认技能按钮在单击时变为不活动状态,或者我只需要对层进行排序?
我目前的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Abilities : MonoBehaviour
{
public Image abilityImage1;
public float cooldown = 5;
bool isCooldown = false;
public Button ability1;
void Start()
{
abilityImage1.fillAmount = 0;
ability1.onClick.AddListener(AbilityUsed);
}
private void AbilityUsed()
{
if (isCooldown)
return;
isCooldown = true;
abilityImage1.fillAmount = 1;
StartCoroutine(LerpCooldownValue());
}
private IEnumerator LerpCooldownValue()
{
float currentTime = 0;
while (currentTime < cooldown)
{
abilityImage1.fillAmount = Mathf.Lerp(1, 0, currentTime /
cooldown);
currentTime += Time.deltaTime;
yield return null;
}
abilityImage1.fillAmount = 0;
isCooldown = false;
}
}第一张图片是我的原始技能按钮,在底部是一个复制品,我做了一个调整颜色,以创建一个叠加效果,以冷却。



谢谢!
发布于 2020-06-17 20:22:04
在Update()方法中使用Lerp而不是更改填充量
void Start()
{
abilityImage1.fillAmount = 0;
ability1.onClick.AddListener(AbilityUsed);
}
private void AbilityUsed()
{
if (isCooldown)
return;
isCooldown = true;
abilityImage1.fillAmount = 1;
StartCoroutine(LerpCooldownValue());
}
private IEnumerator LerpCooldownValue()
{
float currentTime = 0;
while (currentTime < cooldown)
{
abilityImage1.fillAmount = Mathf.Lerp(1, 0, currentTime / cooldown);
currentTime += Time.deltaTime;
yield return null;
}
abilityImage1.fillAmount = 0;
isCooldown = false;
}https://stackoverflow.com/questions/62426772
复制相似问题