手机游戏中技能按钮的冷却功能

问题描述 投票:0回答:1

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

左边的按钮是我的默认技能按钮,右边的按钮是一个副本。默认的技能按钮会放在副本上,所以当我运行游戏时,点击默认的技能按钮时,副本会和默认的技能按钮重叠。

Skill Button Comparison

然而,在我的情况下,副本无法重叠默认技能按钮,所以它不显示冷却时间。

我想知道,我是否需要加入一组代码,让默认技能按钮在点击后变成非活动状态,还是只需要对图层进行排序?

我目前的代码是这样的。

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;
}
}

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

Skill Button Skill button 1Code Inspector

谢谢!

c# function unity3d visual-studio-2010 unityscript
1个回答
1
投票

在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;
}
© www.soinside.com 2019 - 2024. All rights reserved.