Unity技能冷卻的實現:
來源於一個因爲某些原因做廢了的Demo...
很多時候,爲了限制玩家在某一段時間內只能用一次技能,就引入了技能冷卻;
實現思路:點擊按鈕自動根據冷卻時間進入button不可點擊的狀態,以及使灰色遮罩圖片顯示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
/*
*琦玉老師的二弟子
*/
namespace View
{//NameSpace_Start--------------------------------------------------------------------------------------------------------
public class View_PlayerSkillEffect : MonoBehaviour
{
//Class_Start--------------------------------------------------------------------------------------------------------
public float CD_Time = 0f;//技能時間
public Image imgCircle;//外邊的藍圈
public Image imgBW;//遮罩的圖片
//累加器
private float Times = 0f;
//是否遮罩
private bool ButtonMashSwitch = false;
private Button ThisButton;
public Text CDTimeText;
private void Awake()
{
ThisButton = this.GetComponent<Button>();
}
private void Start()
{
imgBW.gameObject.SetActive(false);
}
private void Update()
{
if (ButtonMashSwitch)//核心就在這裏,我們通過一個開關來判定是不是進入冷卻階段
{
imgBW.gameObject.SetActive(true);
Times += Time.deltaTime;
imgCircle.fillAmount = Times / CD_Time;
CDTimeText.text = Math.Round(CD_Time - Times, 1).ToString();
if (Times >= CD_Time)
{
CDTimeText.text = null;
ButtonMashSwitch = false;
imgCircle.fillAmount = 1;
Times = 0f;
imgBW.gameObject.SetActive(false);
ThisButton.interactable = true;
}
}
}
public void SkillTimeStarts()//按鈕的註冊方法
{
ButtonMashSwitch = true;
ThisButton.interactable = false;
}
//Class_End--------------------------------------------------------------------------------------------------------
}
}//NameSpace_End--------------------------------------------------------------------------------------------------------