首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手游中技能按钮的降温功能

手游中技能按钮的降温功能
EN

Stack Overflow用户
提问于 2020-06-17 18:23:24
回答 1查看 258关注 0票数 0

我想包括一个冷却功能,我的技能按钮在我的手机游戏,我目前正在开发。所以我想让玩家每隔几秒钟只能使用一次技能按钮。

左键是我的默认技能键,右键是一个复制键。默认技能按钮将被放置在副本上,因此当我运行游戏时,单击默认技能按钮时,副本将与默认技能按钮重叠。

然而,在我的例子中,副本不能覆盖默认技能按钮,所以它不会显示冷却计时器。

我想知道我是否需要包括一组代码,以允许默认技能按钮在单击时变为不活动状态,或者我只需要对层进行排序?

我目前的代码如下:

代码语言:javascript
复制
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;
}
}

第一张图片是我的原始技能按钮,在底部是一个复制品,我做了一个调整颜色,以创建一个叠加效果,以冷却。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-17 20:22:04

在Update()方法中使用Lerp而不是更改填充量

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62426772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档