每隔x分钟更新Unity AI文本

时间:2017-06-29 09:22:04

标签: c# unity3d scripting

我有一年中的一天,一年中的一年零一小时连同在页面的左上角形成UI文本值。 我使用dayOfYear数组的索引形成上面的内容,然后是年份的数字,后跟一个hourInDay数组的索引。

例如,这会创建以下内容:'1st Janauary 2371 00:00'

我想在现实世界中每隔5分钟移动1小时,然后当它到达23:00时,重置小时数组并将日数组增加到1月2日,依此类推。

显示上述工作正常,但是当我尝试运行一个函数来每五分钟增加一小时时,没有任何反应,但控制台中没有错误。我已经尝试了一个协同例程,我尝试使用invokerepeating函数来增加当前数组索引的时间,我也尝试过这一点,在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();
}

}

1 个答案:

答案 0 :(得分:1)

您需要在增加后设置gameDatetime:

void ProgressTime()
{
    incrementHour();
    gameDateTime = dayNo + "  " + yearNo + "  " + hourNo;
    DateTimeText = GetComponent<UnityEngine.UI.Text>();
    DateTimeText.text = gameDateTime;


}