简体   繁体   English

我怎样才能让“时间”不断更新

[英]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:现在我有一些理论:

  • one is that in the game, just like in real life, it's like with any personal computer user when we open it there is a lockscreen with a clock and we press enter to enter all the time to access the internet and other files on the desktop, so I have the theory that the lockscreen being a GameObject which is like visibility opposite to the desktop, ie if the lockscreen is active the desktop is not visible and vice versa, the clock does not update because let's say if we are on the desktop the lockscreen is disabled and that's why the script doesn't really work and you have to to make two separate ones, one for the lockscreen and one for the desktop, actually for the clock on the desktop一个是在游戏中,就像在现实生活中一样,就像任何个人计算机用户一样,当我们打开它时,会有一个带有时钟的锁屏,我们一直按回车进入以访问互联网和桌面上的其他文件,所以我的理论是锁屏是一个游戏对象,就像桌面的可见性一样,即如果锁屏处于活动状态,桌面是不可见的,反之亦然,时钟不会更新,因为假设我们在桌面上锁屏被禁用,这就是脚本不起作用的原因,您必须制作两个单独的脚本,一个用于锁屏,一个用于桌面,实际上是用于桌面上的时钟
  • or I don't know how to code well或者我不知道如何写好代码

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 一无所知,但似乎您在最初分配timedate后永远不会更新它们的值。 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.

相关问题 如何使一种形式不断覆盖另一种形式? - How can I make one form constantly overlaying another form? 如何使静态类不断更新自己的变量? - How to make a static class update its own variables constantly? Android,C#:如何使我的应用程序在后台不断检查新闻? - Android, C#: How can I make my app constantly check for news in the background? 如何使二维对象不断朝一个方向移动,但在给定水平输入时改变方向但不改变速度? - How can I make 2d object constantly moving in one direction, but when given horizontal input change direction but not change speed? 如何在不影响性能的情况下不断拍摄屏幕快照? - How can I constantly take screen shots without killing performance? 如何在控件聚焦时不断显示工具提示? - How can I display a tooltip constantly while a control is focused? 如何更改游戏中不断生成的对象的速度? - How can I change the speed of objects that are spawning in constantly in my game? 我可以实时更新 C# 控制台输出而不需要不断写入吗? - Can I update C# console output in realtime without constantly writing to it? 如何使用不断变化的数据更新 Observable Collection - How to update an Observable Collection with constantly changing data 如何在更新功能中使用WaitForSeconds不断旋转-Unity - How to constantly rotating with WaitForSeconds in update function - Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM