[英]How I can make "Time" to be update constantly
I am beginner game dev in unity and i start new little project "alone", and in my game i have a little problem.我是统一的初学者游戏开发者,我“独自”开始新的小项目,在我的游戏中我遇到了一个小问题。
My problem is that the time, ie the clock in the game is not synchronized and does not synchronize and I have some suspicions.我的问题是时间,即游戏中的时钟不同步,不同步,我有些怀疑。
I know maybe I'm doing another wrong step, maybe I put a wrong equation in the whole function and nothing works anymore, but to tell you more briefly, I want to make a game in which the game interface is a desktop and the clock on I coded it below, it doesn't work to be updated with time, I waited and tried many solutions that I applied and they didn't work, so I'll leave the code below我知道我可能又做错了一步,也许我在整个函数中放了一个错误的等式,什么都不起作用了,但更简单地说,我想制作一个游戏界面是桌面和时钟的游戏我在下面对其进行了编码,它无法随时间更新,我等待并尝试了我应用的许多解决方案但它们没有用,所以我将代码留在下面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class DateTime : MonoBehaviour
{
public Text timeLockScreenText;
public Text dateLockScreenText;
public Text timeTaskBarText;
string time = System.DateTime.UtcNow.ToLocalTime().ToString("HH:mm");
string date = System.DateTime.UtcNow.ToLocalTime().ToString("MM-dd-yyyy");
// Update is called once per frame
void Update()
{
InvokeRepeating("DateTimeV",0f,1f);
}
public void DateTimeV()
{
timeLockScreenText.text = time;
dateLockScreenText.text = date;
timeTaskBarText.text = timeLockScreenText.text;
}
}
Now I have some theories:现在我有一些理论:
I tried to put in Update() function, in InvokeRepeting, StartCourutine with IEnumerator function, and i dont have the solutions我尝试在 InvokeRepeting 中放入 Update() 函数,StartCourutine 和 IEnumerator 函数,但我没有解决方案
I don't know anything about Unity, but it seems that you're never updating the values of time
and date
after they're initially assigned.我对 Unity 一无所知,但似乎您在最初分配
time
和date
后永远不会更新它们的值。 Perhaps something like this would do the trick, where you just assign the correct values in the method:也许像这样的事情可以解决问题,您只需在方法中分配正确的值:
public void DateTimeV()
{
DateTime utcNow = System.DateTime.UtcNow.ToLocalTime();
timeLockScreenText.text = utcNow.ToString("HH:mm");
dateLockScreenText.text = utcNow.ToString("MM-dd-yyyy");
timeTaskBarText.text = timeLockScreenText.text;
}
Use fixed update, and the code from Rufus L
:使用固定更新,以及来自
Rufus L
的代码:
void FixedUpdate() => DateTimeV();
public void DateTimeV()
{
DateTime utcNow = System.DateTime.UtcNow.ToLocalTime();
timeLockScreenText.text = utcNow.ToString("HH:mm");
dateLockScreenText.text = utcNow.ToString("MM-dd-yyyy");
timeTaskBarText.text = timeLockScreenText.text;
}
What Rufus L said + you would use InvokeRepeating
only once eg in Rufus L所说的 + 你只会使用
InvokeRepeating
一次,例如
private void Start()
{
InvokeRepeating(nameof(DateTimeV), 0f, 1f);
}
or use a simple counter或者使用一个简单的计数器
private float timer;
private void Update ()
{
timer += Time.deltaTime;
if(timer > 1f)
{
timer -= 1f;
DateTimeV();
}
}
or a coroutine或协程
private IEnumerator Start()
{
while(true)
{
DateTimeV();
yield return new WaitForSeconds(1f);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.