协程倒计时

    private Image image;
    int number;

    public float cooldown=2;

    float startTime;
    private void Awake()
    {
        number = int.Parse ( this.name);
        image = this.transform.GetChild(0).GetComponent();
        image.fillAmount = 0f;

        SkillTime.instanceSkill += SkillCooldown;
    }
    bool IsSkill = true ;

    private void SkillCooldown(int index)
    {
        
        if(index ==number && IsSkill)
        {
            StartCoroutine(CooldownSkill());
        }
    }

    private IEnumerator CooldownSkill()
    {
        float a = 0;
        startTime = Time.time;
        image.fillAmount = 1;
        IsSkill = false;
        float temp = cooldown;

         while (image.fillAmount > 0 )
        {                    
            a += Time.deltaTime;
            image.fillAmount = 1-(a/2);         
            yield return null ;
            
        }        
        IsSkill = true;
        yield return null;
    }

你可能感兴趣的:(Unity)