超级实用Unity倒计时

2019-07-13  本文已影响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();

            }

        }

    }

上一篇 下一篇

猜你喜欢

热点阅读