返回 登录
0

技能冷却更新UI使用的两种倒计时

Unity引擎、NGUI。哪一种更好呢?
1、Update中

using UnityEngine;
using System.Collections;

public class SkillCDUpdatce : SkillCDShowIconBase {

    private bool bIsInCD;

    public float fCD;
    private float fTimeCount;

    public UISprite _Sprite;

    void Update(){
        if(bIsInCD && null != _Sprite){
            fTimeCount += Time.deltaTime;
            if(fTimeCount > fCD){
                fTimeCount = 0;
                bIsInCD = false;
                _Sprite.gameObject.SetActive(false);
            }else{
                _Sprite.fillAmount = 1 - fTimeCount / fCD;//倒计时UI更新
            }
        }
    }

    public override void StartCD(){
        if(!bIsInCD && null != _Sprite){//只有技能在可以使用时才会进入CD
            this.bIsInCD = true;
            _Sprite.gameObject.SetActive(true);
        }
    }
}

2、while中

using UnityEngine;
using System.Collections;

public class SkillCDWhile : SkillCDShowIconBase {

    private bool bIsInCD;

    public float fCD;
    private float fTimeCount;

    public UISprite _Sprite;

    public override void StartCD(){
        if(!bIsInCD && null != _Sprite){
            bIsInCD = true;
            StartCoroutine(Work());
            _Sprite.gameObject.SetActive(true);
        }
    }

    private IEnumerator Work(){
        while(bIsInCD && null != _Sprite){
            fTimeCount += Time.deltaTime;
            if(fTimeCount > fCD){
                fTimeCount = 0;
                bIsInCD = false;
                _Sprite.gameObject.SetActive(false);
            }else{
                _Sprite.fillAmount = 1 - fTimeCount / fCD; //倒计时UI更新
                yield return 0;
            }
        }
    }
}

3、右边为Update、左边为while
图片描述
4、源码git地址

评论