简体   繁体   English

Unity 3d 游戏开始和结束时间

[英]Unity 3d Game Start and End Time

I have made a timer script which starts timer when game starts.我制作了一个计时器脚本,它在游戏开始时启动计时器。 Thats fine, now what I want is when i press the "CurrentTime" button then the current time should be displayed on UI Text.很好,现在我想要的是当我按下“CurrentTime”按钮时,当前时间应该显示在 UI 文本上。 For example game has started at "24:00" and after 4 minutes of play when I press the "CurrentTime" button then UI Text should display "24:04. Any help? here is my code.例如,游戏已在“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);
    }
}  

If you actually wanted the current real time it doesn't depend on the time the app was started but rather the system time itself.如果你真的想要当前的实时时间,它不取决于应用程序的启动时间,而是系统时间本身。

Then all you need to do would be simply using System.DataTime and in particular DateTime.Now like然后你需要做的就是简单地使用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}";     
    }
}

You wouldn't need the rest of your script at all because the system itself runs the time.您根本不需要脚本的其余部分,因为系统本身运行时间。


If what you rather want is a counter I would simply do something like如果你更想要的是一个计数器,我会简单地做类似的事情

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