Unity中的计时器

计时器简单实现(有问题懒得改,下个标题是修改后的)

在GameFramework中协程不知道为啥用不了,只能用update来写计时器了
首先设置一下时分秒数值,这个必须提前写好,不能封方法,因为放进Update里之后每帧都会重新赋值,不能执行倒计时显示了
float a = 00f;
float b = 59f;
float c = 60f;
封方法
传个显示的字符,传个要展示的text

private void _jishiqi(string d,Text jingli)
        {

            c -= Time.deltaTime;
            if (c <= 0)
            {
                b -= 1;
                c = 60;
            }
            if (b <= 0)
            {
                a -= 1;
                b = 60;
            }
            jingli.text = d + a + ":" + b + ":" + (int)c;
            
            
            //
        }

放进update里
Unity中的计时器_第1张图片

当前系统时间

Unity中的计时器_第2张图片

例精力全部恢复时间=系统时间+倒计时时间

 private void Jingliquanbuhuifu(string e, Text allHuifu)
        {
            if (b != 0)
            {
                int nowc = (int)c + DateTime.Now.Second;
                int nowb = (int)b + DateTime.Now.Minute;
                int nowa = (int)a + DateTime.Now.Hour;
                if (nowc >= 60)
                {
                    nowb += 1;
                    nowc = nowc - 60;
                }
                if (nowb >= 60)
                {
                    nowa += 1;
                    nowb = nowb - 60;
                }
                if (nowa >= 24)
                {
                    nowa = nowa - 24;
                    allHuifu.text = e + "明日" + nowa + ":" + nowb;
                }
                else
                {
                    allHuifu.text = e + "今日" + nowa + ":" + nowb;
                }
            }
            else
            {
                allHuifu.text = "已满";
            }

        }

不要放进update,只用调用一次就行,这里放进了Onopen里

Unity中的计时器_第3张图片

修改后的

private void _jishiqi(string d,Text jingli)
        {
            c -= Time.deltaTime;

            jingli.text = d + (int)c / 3600 + ":" + (int)c/60%60 + ":"+ (int)c % 60;
            if (c<=0)
            {
                c = 0;
                _jingliAllhuifu.text = "已满";
            }
           
        }
        //精力全部恢复
        float jinglizhi = 0.1f;
        float a;
        float b ;
        float c ;
        private void Jingliquanbuhuifu(string e, Text allHuifu)
        {
            c = 30 * jinglizhi * 60;
            if (c>0)
            {
                int nowc = (int)c % 60 + DateTime.Now.Second;
                int nowb = (int)c / 60 % 60 + DateTime.Now.Minute;
                int nowa = (int)c / 3600 + DateTime.Now.Hour;
                if (nowc >= 60)
                {
                    nowb += 1;
                    nowc = nowc - 60;
                }
                if (nowb >= 60)
                {
                    nowa += 1;
                    nowb = nowb - 60;
                }
                if (nowa >= 24)
                {
                    nowa = nowa - 24;
                    allHuifu.text = e + "明日" + nowa + ":" + nowb;
                }
                else 
                {
                    allHuifu.text = e + "今日" + nowa + ":" + nowb;
                }
            }
            else if(c<=0)
            {
                
                    allHuifu.text = "已满";
                
            }

        }

你可能感兴趣的:(GameFramework框架)