簡體   English   中英

Unity 3d 游戲開始和結束時間

[英]Unity 3d Game Start and End Time

我制作了一個計時器腳本,它在游戲開始時啟動計時器。 很好,現在我想要的是當我按下“CurrentTime”按鈕時,當前時間應該顯示在 UI 文本上。 例如,游戲已在“24:00”開始,當我按下“CurrentTime”按鈕播放 4 分鍾后,UI 文本應顯示“24:04。有幫助嗎?這是我的代碼。

public class Timer : MonoBehaviour {
    public int  Hours = 0;
    public int  Minutes = 0;
    public Text    m_text;
    private float   timestart;
    public Text ending;

    void Awake()
    {
        timestart = GetInitialTime();
    }
    void Start () {
        m_text.text = timestart.ToString();
    }

    private void Update()
    {
        if (timestart > 0f)
        {
            //  Update countdown clock
            timestart += Time.deltaTime * 0.25f;
            Hours = GetLeftHours();
            Minutes = GetLeftMinutes();

            //  Show current clock
            if (timestart > 0f)
            {
                m_text.text = Hours + ":" + Minutes.ToString("00");
            }
            else
            {
                //  The countdown clock has finished
                m_text.text = "00:00";
            }
        }
        ending.text = Hours + ":" + Minutes.ToString("00");
    }

    private float GetInitialTime()
    {
        return Hours * 60f + Minutes;
    }

    private int GetLeftHours()
    {
        return Mathf.FloorToInt(timestart / 60f);
    }

    private int GetLeftMinutes()
    {
        return Mathf.FloorToInt(timestart % 60f);
    }
}  

如果你真的想要當前的實時時間,它不取決於應用程序的啟動時間,而是系統時間本身。

然后你需要做的就是簡單地使用System.DataTime ,特別是DateTime.Now

using System;

public class Timer : MonoBehaviour
{
    public Text m_text;

    public void GetCurrentTime()
    {
        var time = DateTime.Now;
        m_text.text = $"{time.Hour:00}:{time.Minute:00}";     
    }
}

您根本不需要腳本的其余部分,因為系統本身運行時間。


如果你更想要的是一個計數器,我會簡單地做類似的事情

public class Timer : MonoBehaviour
{
    public Text m_text;

    public float Seconds;
    public int Hours;
    public int Minutes;

    private void Start()
    {
        // in case you want to start the timer automatically
        StartTimer();
    }       

    public void StartTimer()
    {
        StopAllCoroutines();
        StartCoroutine(TimerRoutine());
    }

    public void StopTimer()
    {
        StopAllCoroutines();
        GetCurrentTime();
    }

    private IEnumerator TimerRoutine()
    {
        Seconds = 0;
        Hours = 0;
        Minutes = 0;
        while(true)
        {
            Seconds += Time.deltaTime;
            if(Seconds >= 60)
            {
                Seconds -= 60.0f;
                Minutes += 1;
                if(Minutes >= 60)
                {
                    Minutes -= 60;
                    Hours += 1;
                }
            }

            GetCurrentTime();
            yield return null;
        }
    }

    public void GetCurrentTime()
    {
        m_text.text = $"{Hours:00}:{Minutes:00}:{Seconds:00}"
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM