【发布时间】: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);
}
}
【问题讨论】: