UGUI血条渐渐减掉实现

来源:互联网 发布:网络词种草是什么意思 编辑:程序博客网 时间:2024/04/30 10:00

好久没写文章了。那天有人问我游戏人物血条如何慢慢减掉。今天写一下吧。首先上个动态图,看效果:

 

在之前的文章中讲过如何实现技能冷却的效果(上图中技能冷却效果),血条的慢慢减小原理和技能冷却的是一样的。

 

下面是具体步骤: 


1.首先先新建一个scrollbar 作为血条,然后把 Handle的颜色改成红色,并且把ImageType改成Filled.,把填充方式改成水平的(见下图:)

 


2.然后开始用脚本控制血条
我脚本绑定的位置是技能冷却的图片,然后把Handle拖到指定位置(如下图:)
 

技能冷却代码和血条渐渐减小代码如下

(代码中写了详细的注释这里不再赘述):

using UnityEngine;using System.Collections;using UnityEditor;using UnityEngine.UI;public class skillcd : MonoBehaviour{    public Image hps;    private Image spcd;    private bool cooling, hpstart;    private float hurt = 0.2f, all = 1;    // Use this for initialization    void Start()    {        hps.fillAmount = 1f;//血量         spcd = this.GetComponent<Image>();//获取组件方法        spcd.fillAmount = 0;//冷却值    }     // Update is called once per frame    void Update()    {        if (cooling == false && Input.GetKeyDown(KeyCode.R))//放技能        {             hpstart = true;            spcd.fillAmount = 1f;            cooling = true;//冷却条件          }         if (cooling)        {             spcd.fillAmount -= 0.2f * Time.deltaTime;            if (spcd.fillAmount < 0.01f)//冷却完毕            {                spcd.fillAmount = 0;                cooling = false;            }        }                  //血量逻辑开始        if (hpstart)        {            Debug.Log("血量:" + hps.fillAmount);             hps.fillAmount = (hps.fillAmount -= 0.1f * Time.deltaTime) * hps.fillAmount / (hps.fillAmount);             if (hps.fillAmount <= (all - hurt))//当前血量值 小于等于 目标血量值            {                Debug.Log("ting");                 hpstart = false;                all = hps.fillAmount;//总血量值 被赋值 当前血量值            }             Debug.Log("血量2:" + hps.fillAmount);        }    }}


就这样就实现了这个功能,So Easy!


0 0
原创粉丝点击