C#在Unity游戏引擎中开箱后的倒计时
C# countdown after opening box in the Unity game engine
我需要在打开盒子时访问 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;
}
}
我不知道你为什么在更新盒子时要实现你的打开逻辑,你应该把它们放在 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
{
// ...
}
}
}
我需要在打开盒子时访问 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;
}
}
我不知道你为什么在更新盒子时要实现你的打开逻辑,你应该把它们放在 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
{
// ...
}
}
}