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裏
在這裏插入圖片描述

當前系統時間

在這裏插入圖片描述

例精力全部恢復時間=系統時間+倒計時時間

 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裏

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章