NGUI系列教程六(技能冷却的CD效果)

今天来学习一下,如何利用NGUI做技能冷却的CD效果。

1,首先按之前的教程Create Your UI,Camera为Simple 2D

最终如下图:

2,添加一个按钮,Background 为一张半透明贴图,更名为“CDSprite”,

设置CDSprite的Sprite Type为Filled

3,添加一个Sprite,重命名为:IconSprite,该Sprite为技能图标。

4,编写脚本:

using UnityEngine;
using System.Collections;

public class CDEffect : MonoBehaviour {

    UISprite sprite;
    private bool cd_bool = false;
    private float CDTime = 5.0f; //CD时间
    private float CurrentTime;  //剩余CD时间
    // Use this for initialization
    void Start () {
        sprite = gameObject.GetComponentInChildren<UISprite>(); //获取按钮的UISprite
        sprite.fillAmount = 0;

    }
    void OnClick()
    {
        if (cd_bool == false)
        {
            Debug.Log("执行某操作。。。");
            cd_bool = true;
            sprite.fillAmount = 1;
            CurrentTime = CDTime;
        }
    }
    // Update is called once per frame
    void Update () {
        if (cd_bool)
        {
            CurrentTime -= Time.deltaTime;
            if (CurrentTime < 0)
            {
                CurrentTime = 0;
            }
            sprite.fillAmount = CurrentTime / CDTime;
            if (sprite.fillAmount == 0)
            {
                cd_bool = false;
            }

        }
    }
}

5,把脚本绑定在Button上,运行,点击按钮,

posted @ 2013-10-21 11:19  martianzone  阅读(1172)  评论(0编辑  收藏  举报