【问题标题】:Unity 3d Game Start and End TimeUnity 3d 游戏开始和结束时间
【发布时间】:2020-03-09 07:39:26
【问题描述】:

我制作了一个计时器脚本,它在游戏开始时启动计时器。很好,现在我想要的是当我按下“CurrentTime”按钮时,当前时间应该显示在 UI 文本上。例如,游戏从“24:00”开始,玩了 4 分钟后,当我按下“CurrentTime”按钮时,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);
    }
}  

【问题讨论】:

    标签: c# unity3d time timer


    【解决方案1】:

    如果您真的想要当前的实时时间,它并不取决于应用程序的启动时间,而是取决于系统时间本身。

    那么你需要做的就是简单地使用System.DataTime,尤其是DateTime.Now like

    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}"
        }
    }
    

    【讨论】:

    • 不,我不想要当前时间。比赛时间总是从 24:00 开始。简而言之,当我开始游戏倒计时从 0 到 ++ 开始。当我的车到达终点并且倒计时为 5 时,计时器应停止并且 ui 文本应显示“5”。
    • @AwaisAhmed 不过,这不是您在问题中提出的问题……请参阅更新的答案。希望对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多