美文网首页
超级实用Unity倒计时

超级实用Unity倒计时

作者: 浪荡少年 | 来源:发表于2019-07-13 16:33 被阅读0次

创建一个Time_count方法,在start方法中使用InvokeRepeating方法,在2秒后每1秒执行一次Time_count方法,在里面减去1并更新Text文本显示,当数值小于0时就停止CancelInvoke调用。   

int count_down = 9;

void Start () {

GetComponent<UnityEngine.UI.Text> ().text = ""+count_down;

InvokeRepeating ("Time_count", 1.0f, 1.0F);

}

void Time_count()

if (count_down > 0) {

count_down--;

GetComponent<UnityEngine.UI.Text> ().text = "" + count_down;

} else {

CancelInvoke ();

}

}

加分和秒 栗子: 01:30s

    public Text timerText;

    int count_down = 30;

    int col = 1;

// Use this for initialization

void Start () {

        OnJiFenPanelClick(true);

        timerText.text = "0" + col + ":" + count_down + "s";

        InvokeRepeating("Time_count", 1.0f, 1.0F);

    }

    void Time_count()

    {

        if (count_down > 0)

        {

            count_down--;

            if (count_down > 9)

            {

                timerText.text = "0" + col + ":" + count_down + "s";

            }

            else

            {

                timerText.text = "0" + col + ":0" + count_down + "s";

            }

        }

        else

        {

            col--;

            count_down = 59;

            timerText.text = "0" + col + ":" + count_down + "s";

            if (col < 0)

            {

                CancelInvoke();

            }

        }

    }

相关文章

网友评论

      本文标题:超级实用Unity倒计时

      本文链接:https://www.haomeiwen.com/subject/yxbfmqtx.html