首先是效果展示,与上一篇圆形冷却效果https://mp.csdn.net/mp_blog/creation/editor/126068249如出一辙,需要的朋友点击连接即可。
圆形冷却效果见上一篇文章,效果如下:
下面开始正文介绍:
第一步准两张圆形图片,实际圆环是通过两张不同大小图片叠加,对下层图片进行填充。
第二步,Hierarchy中新建button,image,text等。结构如下:
countdown为canves,topcircle为蓝色image,circlepropessbar为白色圆形,text为冷却数,cancle为取消按钮。
各个ui物体inspector属性如下:
第三步创建医疗包按钮, Resumeblood脚本挂载在此按钮上。
全部脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Resumeblood : MonoBehaviour
{
public GameObject player;//玩家
private Animator Anim;//动画
public GameObject lifeblood;//玩家生命值
private float currentTime; //当前冷却时间
private Image coolingImage;//冷却填充图片
private float coolingTimer;//冷却总时间
public GameObject timedown;//冷却图片
public int medicinenum;//医疗包数量
public int currmedicinenum;//当前医疗包数
private Text currmedicinenumtext;//医疗包数text显示
private Text countdowntext;//倒计时显示
// Start is called before the first frame update
void Start()
{
timedown.SetActive(false);//初始倒计时不显示,点击医疗包后显示
currmedicinenumtext=transform.GetChild(2).GetComponent<Text>();//获取医疗包数显示
countdowntext= timedown.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();//获取冷却时间数显示
coolingImage = timedown.transform.GetChild(0).GetChild(0).GetComponent<Image>();//获取冷却填充图片
coolingImage.fillAmount = 1.0f;
coolingImage.raycastTarget = false;
currmedicinenum = medicinenum;
}
void Awake()
{
Anim = player.GetComponent<Animator>();//玩家动画
}
// Update is called once per frame
void Update()
{
if(timedown.active == true)//
{
UpdateImage();
}
currmedicinenumtext.text = currmedicinenum.ToString();
string countdowntextvalue = currentTime + "";
countdowntext.text = countdowntextvalue;
}
public void OnBtnClickSkill(float timer)//参数冷却时间
{
if (currmedicinenum > 0)
{
timedown.SetActive(true);
coolingTimer = timer;
currentTime = timer;
coolingImage.fillAmount = 1.0f;
Anim.SetBool("Treat",true);
}
else
{
Debug.Log("没有医疗包了");
}
}
public void cancel()
{
timedown.active = false;
Anim.SetBool("Treat", false);
}
private void UpdateImage()
{
if (currentTime >= 0)
{
currentTime -= Time.deltaTime;
coolingImage.fillAmount = currentTime / coolingTimer;
if (coolingImage.fillAmount != 0)
{
coolingImage.raycastTarget = true;
}
else
{
lifeblood.TryGetComponent<Bloodcontrol>(out Bloodcontrol bloodcontrol);
bloodcontrol.resumeblood(20);//恢复生命值
coolingImage.raycastTarget = false;
timedown.SetActive(false);
currmedicinenum -= 1;//医疗包数量减一
Anim.SetBool("Treat", false);
}
}
}
}