Unity中传入任意数,转换成分,秒,并进行倒计时换算..(两种方式)

第一种方式是利用Unity中的协程,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DemoTest : MonoBehaviour
{
    public Text text;
    //测试用的数字
    public int MyTime = 60;

    void Start ()
    {
        //开启协程
        StartCoroutine (StartUpdate ());
    }
    //一秒钟减去1
    IEnumerator StartUpdate ()
    {
        while (true) {
            if (MyTime > 0) {
                MyTime -= 1;
                text.text = UpdateTime (MyTime);
            } else {
                break;
            }
            yield return new WaitForSeconds (1f);
        }
    }

    /// <summary>
    /// 时间换算
    /// </summary>
    /// <returns>The time.</returns>
    /// <param name="inputTime">输入的时间</param>
    string UpdateTime (int inputTime)
    {
        string temp;
        int minute, seconds;
        minute = (int)(inputTime / 60);
        Debug.Log ("minute = " + minute);
        seconds = inputTime % 60;
        Debug.Log ("seconds = " + seconds);
//         这样的话,当minute<0的时候只有一个数字,可能有的需求上不允许,所以就换一种方式

//        if (seconds >= 10) {
//            temp = "0" + minute + ":" + seconds;
//        } else {
//            temp = minute + ":" + seconds;
//        }

        //优化版,利用三目运算符进行取值,这样更好的实现倒计时
        string minTemp = (minute < 10) ? "0" + minute : minute.ToString ();
        string secTemp = (seconds < 10) ? "0" + seconds : seconds.ToString ();
        temp = minTemp + ":" + secTemp;

        return temp;
    }

}

第二种方式,利用Update,原理是一样的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DemoTest : MonoBehaviour
{
    public Text text;
    //测试用的数字
    public int MyTime = 60;

    float timer = 0;
    float timerInterval = 1f;



    void Update ()
    {
        timer += Time.deltaTime;
        if (timer >= timerInterval) {
            timer = 0;
            MyTime -= (int)timerInterval;
            if (MyTime < 0) {
                return;
            }
            text.text = UpdateTime (MyTime);
        }
    }

    /// <summary>
    /// 时间换算
    /// </summary>
    /// <returns>The time.</returns>
    /// <param name="inputTime">输入的时间</param>
    string UpdateTime (int inputTime)
    {
        string temp;
        int minute, seconds;
        minute = (int)(inputTime / 60);
        Debug.Log ("minute = " + minute);
        seconds = inputTime % 60;
        Debug.Log ("seconds = " + seconds);
        //         这样的话,当minute<0的时候只有一个数字,可能有的需求上不允许,所以就换一种方式

        //        if (seconds >= 10) {
        //            temp = "0" + minute + ":" + seconds;
        //        } else {
        //            temp = minute + ":" + seconds;
        //        }

        //优化版,利用三目运算符进行取值,这样更好的实现倒计时
        string minTemp = (minute < 10) ? "0" + minute : minute.ToString ();
        string secTemp = (seconds < 10) ? "0" + seconds : seconds.ToString ();
        temp = minTemp + ":" + secTemp;

        return temp;
    }
}

 

posted @ 2018-08-01 11:10  不够自律的人  阅读(1808)  评论(0编辑  收藏  举报