C# 时间管理(使用 Unity 2D)
C# Time Management (with Unity 2D)
我 运行 在将 C#/Unity 与小型倒数计时器结合使用时遇到了一个小问题。只要总 timeToDisplay 的数量不是太大(例如超过一天),倒计时就可以按预期正常工作
r/Unity2D - 求助:倒计时st运行gely在剩余时间过多时停止
如您所见,用户可以为该倒计时添加时间,这(再次)工作正常,直到时间太多。
使用 TextMeshPro 和 TextMeshPro 按钮。
Image of Countdown + Buttons to add Seconds/Minutes/Hours/Days
但是...这是代码:
using UnityEngine;
using TMPro;
public class Controller : MonoBehaviour
{
public float timeValue = 78000;
public TMP_Text timerText;
// I also tried FixedUpdate, but error still occured
void Update()
{
if (timeValue > 0)
{
timeValue -= Time.deltaTime;
}
else
{
timeValue = 0;
}
DisplayTime(timeValue);
}
void DisplayTime(float timeToDisplay)
{
float days = Mathf.FloorToInt(timeToDisplay / 86400);
timeToDisplay = timeToDisplay % 86400;
float hours = Mathf.FloorToInt(timeToDisplay / 3600);
timeToDisplay = timeToDisplay % 3600;
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
timeToDisplay = timeToDisplay % 60;
float seconds = Mathf.FloorToInt(timeToDisplay);
if (seconds < 0)
{
seconds = 0;
}
timerText.text = string.Format("{0:00} days {1:00} hours {2:00} minutes {3:00} seconds", days, hours, minutes, seconds);
}
public void AddDay()
{
/* 86400 seconds/day */
timeValue += 86400;
}
public void AddHour()
{
/* 3600 seconds/hour */
timeValue += 3600;
}
public void AddMinute()
{
timeValue += 60;
}
public void AddSecond()
{
timeValue += 1;
}
}
有人知道我在这里遗漏了什么吗?
这里的问题:timeValue -= Time.deltaTime
,浮动有点偏差
public float timeValue = 78000;
float beginTime = 0;
void Start()
{
beginTime = Time.time;
}
// I also tried FixedUpdate, but error still occured
void Update()
{
float usedTime = Time.time - beginTime;
if( timeValue - usedTime > 0 )
{
DisplayTime(timeValue - usedTime);
}
else
{
DisplayTime(0);
}
}
我 运行 在将 C#/Unity 与小型倒数计时器结合使用时遇到了一个小问题。只要总 timeToDisplay 的数量不是太大(例如超过一天),倒计时就可以按预期正常工作
r/Unity2D - 求助:倒计时st运行gely在剩余时间过多时停止 如您所见,用户可以为该倒计时添加时间,这(再次)工作正常,直到时间太多。
使用 TextMeshPro 和 TextMeshPro 按钮。
Image of Countdown + Buttons to add Seconds/Minutes/Hours/Days
但是...这是代码:
using UnityEngine;
using TMPro;
public class Controller : MonoBehaviour
{
public float timeValue = 78000;
public TMP_Text timerText;
// I also tried FixedUpdate, but error still occured
void Update()
{
if (timeValue > 0)
{
timeValue -= Time.deltaTime;
}
else
{
timeValue = 0;
}
DisplayTime(timeValue);
}
void DisplayTime(float timeToDisplay)
{
float days = Mathf.FloorToInt(timeToDisplay / 86400);
timeToDisplay = timeToDisplay % 86400;
float hours = Mathf.FloorToInt(timeToDisplay / 3600);
timeToDisplay = timeToDisplay % 3600;
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
timeToDisplay = timeToDisplay % 60;
float seconds = Mathf.FloorToInt(timeToDisplay);
if (seconds < 0)
{
seconds = 0;
}
timerText.text = string.Format("{0:00} days {1:00} hours {2:00} minutes {3:00} seconds", days, hours, minutes, seconds);
}
public void AddDay()
{
/* 86400 seconds/day */
timeValue += 86400;
}
public void AddHour()
{
/* 3600 seconds/hour */
timeValue += 3600;
}
public void AddMinute()
{
timeValue += 60;
}
public void AddSecond()
{
timeValue += 1;
}
}
有人知道我在这里遗漏了什么吗?
这里的问题:timeValue -= Time.deltaTime
,浮动有点偏差
public float timeValue = 78000;
float beginTime = 0;
void Start()
{
beginTime = Time.time;
}
// I also tried FixedUpdate, but error still occured
void Update()
{
float usedTime = Time.time - beginTime;
if( timeValue - usedTime > 0 )
{
DisplayTime(timeValue - usedTime);
}
else
{
DisplayTime(0);
}
}