• 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上,运行,点击按钮,

  • 相关阅读:
    小程序(二)
    React 之 项目搭建
    mac 终端 常用命令
    css 之 动画(翻转动画例子)
    css 之 单位
    Git 常见操作
    css 之 页面常用布局
    mac版vscode快捷键
    JSX
    Rem适配原理
  • 原文地址:https://www.cnblogs.com/martianzone/p/3380240.html
Copyright © 2020-2023  润新知