手游技能键降温功能 [英] Cool down function for skill button in mobile game

查看:25
本文介绍了手游技能键降温功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to include a cool down function to my skill button in my mobile game that I am currently developing. So I wanted to allow the player to only be able to use the skill button once every few seconds.

左侧按钮是我的默认技能按钮,右侧按钮是重复的.默认技能按钮将放置在副本上,因此当我运行游戏时,单击默认技能按钮后,副本将与默认技能按钮重叠.

Left button is my default skill button and right one is a duplicate. The default skill button will be placed over the duplicate so when I run the game, upon clicking on the default skill button, the duplicate will overlap the default skill button.

但是,在我的情况下,副本无法与默认技能按钮重叠,因此不会显示冷却计时器.

However, in my case the duplicate is not able to overlap the default skill button so it does not show the cool down timer.

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

I am wondering if I need to include a set of codes to allow the default skill button to turn inactive upon clicking or do I just have to sort the layers?

我目前的代码如下:

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

第一张图片是我原来的技能按钮,底部是复制的,我调整了颜色来创建冷却叠加效果.

The first picture is my original skill button and on the bottom is a duplicate which I made to adjust the color to create an overlay effect for the cool down.

谢谢!

推荐答案

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

Use Lerp instead of changing fill amount in Update() method

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

这篇关于手游技能键降温功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆