Unity制作游戏中技能使用后转CD的场景

来源:互联网 发布:虚拟机网络连接模式 编辑:程序博客网 时间:2024/06/07 17:35

这里写图片描述

因为我们需要用鼠标点击图片让它重新转CD,所以我们需要创建一个Button,然后把Button的子物体Test删除掉,加一个Image为他的子物体。
这里写图片描述

然后我们把图片(这里图片必须是 Sprite (2D and UI)格式的)拖入到Button 的Source Image中,同时拖入到Image的Source Image中,然后在Image中他调整它的颜色,并且改变它的Image Type为Filled,这里颜色自己调整,设置如下图
这里写图片描述

为了达到点击图片转CD的效果写了脚本:

using UnityEngine;using System.Collections;using UnityEngine.UI;public class CDHyp : MonoBehaviour {    //剩余时间    public float leftTime;    //CD转完的总时间    public float totalTime;    private Image effectImage;    private Button cdButton;    // Use this for initialization    void Start () {        effectImage = transform.FindChild("Image").GetComponent<Image>();    //    effectImage = GetComponent<Image>();        leftTime = totalTime;        cdButton = transform.GetComponent<Button>();        cdButton.interactable = false;    }    // Update is called once per frame    void Update () {        leftTime -= Time.deltaTime;        if (effectImage.fillAmount>0)        {            effectImage.fillAmount = leftTime / totalTime;        }        else        {            effectImage.fillAmount = 0;            cdButton.interactable = true;        }    }    public void FireSkillHyp()    {        Debug.Log("技能使用");        leftTime = totalTime;        cdButton.interactable = false;        effectImage.fillAmount = 1;    }}

另外,这里我们还需要把脚本中FireSkillHyp这个事件添加到Button中
这里写图片描述