【发布时间】:2017-06-29 17:32:53
【问题描述】:
我有一年中的一天,一年中的一天和一天中的一个小时,在页面的左上角形成一个 UI 文本值。 我使用 dayOfYear 数组中的索引形成上述内容,然后是年份的数字,然后是 hourInDay 数组的索引。
例如,这将创建以下内容:'1st Janauary 2371 00:00'
我想在现实世界时间中每 5 分钟将小时移动 1,然后当它到达 23:00 时,重置小时数组并增加日数组以显示 1 月 2 日,依此类推。
显示上述内容正常,但是当我尝试运行一个函数以每五分钟增加一小时时,没有任何反应,但控制台中没有错误。我尝试了一个协同例程,我尝试在一个函数上调用重复,该函数将当前数组索引增加一小时,我还尝试在此之上,在 Update() 中再次设置对 UI 文本的引用;我还参考了 UnityEngine.UI。
我的代码如下。谁能告诉我哪里出错了?就像我说的,当我开始游戏时,会根据连接的数组索引显示正确的日期,但 5 秒后没有任何进展。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PassageOfTime : MonoBehaviour
{
public float yearNo;
public string dayNo;
public int currentDay;
public string hourNo;
public int currentHour;
public string gameDateTime;
public Text DateTimeText;
string[] hourInDay = new string[]
{
"00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00"
};
string[] dayInYear = new string[]
{
"January 1st",
"January 2nd",
"January 3rd",
"January 4th",
"January 5th",
"January 6th",
"January 7th",
"January 8th",
"January 9th",
"January 10th",
"January 11th",
"January 12th",
"January 13th",
"January 14th",
"January 15th",
"January 16th",
"January 17th",
"January 18th",
"January 19th",
"January 20th",
"January 21st",
"January 22nd",
"January 23rd",
"January 24th",
"January 25th",
"January 26th",
"January 27th",
"January 28th",
"January 29th",
"January 30th",
"January 31st",
"February 1st",
"Feburary 2nd"
};
void incrementYear()
{
yearNo += 1;
}
void incrementDay()
{
currentDay += 1;
currentHour = 0;
}
void incrementHour()
{
currentHour += 1;
}
void ProgressTime()
{
incrementHour();
DateTimeText = GetComponent<UnityEngine.UI.Text>();
DateTimeText.text = gameDateTime;
}
// Use this for initialization
void Start()
{
DateTimeText = GetComponent<UnityEngine.UI.Text>();
currentDay = 0;
currentHour = 0;
dayNo = dayInYear[currentDay];
hourNo = hourInDay[currentHour];
yearNo = 2371;
gameDateTime = dayNo + " " + yearNo + " " + hourNo;
DateTimeText.text= gameDateTime.ToString();
InvokeRepeating("ProgressTime", 5.0f, 5.0f);
}
// Update is called once per frame
void Update()
{
DateTimeText.text = gameDateTime.ToString();
}
}
【问题讨论】:
-
但是你在
Start函数中修改了一次gameDateTime。你永远不会再改变它。你想让它自动更新吗?也许您应该将gameDateTime = dayNo + " " + yearNo + " " + hourNo;放入其中一个偶尔更新的函数中。