我需要在打开一个盒子并保存它时访问PC或Android系统时间,然后从那个时间开始倒计时5分钟。通过5分钟后,重新启用按钮以打开该框。我尝试了很多方法,但都导致死路一条。
public class Test : MonoBehaviour
{
public Button boxButton;
long previousOpenedBox;
private void Update()
{
if (!boxButton.IsInteractable())
{
long diff = (DateTime.Now.Ticks - previousOpenedBox);
//start counting down the time to re-enable the button.
}
}
public void BoxClicked()
{
long previousOpenedBox = DateTime.Now.Ticks;
boxButton.interactable = false;
}
}
答案 0 :(得分:2)
我不知道您在更新框时实施开放逻辑的原因,您应该将它们放在BoxClicked
方法中。这个最小的例子展示了如何处理阻塞延迟,并指出你正确的路径。
public class Test : MonoBehaviour
{
private DateTime m_LastOpening;
public Button m_BoxButton;
public void BoxClicked()
{
DateTime now = DateTime.Now;
// 5 minutes elapsed, you can open the box
if ((now - m_LastOpening).TotalMinutes > 5)
{
m_LastOpening = now;
m_BoxButton.interactable = false;
}
else // otherwise you have to wait
{
// ...
}
}
}