public class DateTimeText : MonoBehaviour { public StringBuilder StrBuilder { get; set; } = new StringBuilder(); public Text m_dateText; public Text m_timeText; public Text m_weekText; DateTime m_lastDateTime; public DateTime CurrentDateTime { get; private set; } = DateTime.Now; void Start() { if (m_dateText != null) { m_dateText.text = GetDate(); } m_lastDateTime = DateTime.Now; } private string GetDate() { return CurrentDateTime.ToString(DateTimeFormat.DateFormat); } private string GetTime() { return CurrentDateTime.ToString(DateTimeFormat.TimeFormat); } private string GetWeek() { return CurrentDateTime.ToString("dddd"); } float m_interval = 0; void Update() { CurrentDateTime=DateTime.Now; m_interval += Time.deltaTime; if (m_interval >= 0.5f) { m_interval -= 0.5f; DateTime time = CurrentDateTime; if (m_dateText != null) { if (m_lastDateTime.Day != time.Day) { m_dateText.text = GetDate(); m_lastDateTime = time; } } if (m_timeText != null) { if (m_lastDateTime.Second != time.Second) { m_timeText.text = GetTime(); m_lastDateTime = time; } } if (m_weekText!=null) { if (m_lastDateTime.DayOfWeek!=time.DayOfWeek) { m_weekText.text = GetWeek(); m_lastDateTime = time; } } } } } public class DateTimeFormat { public const string Moday = "Moday"; public const string Tuesday = "Tuesday"; public const string Wednesday = "Wednesday"; public const string Thursday = "Thursday"; public const string Friday = "Friday"; public const string Saturday = "Saturday"; public const string Sunday = "Sunday"; public const string DateFormat = "yyyy/MM/dd"; public const string TimeFormat = "HH:mm:ss"; }这段代码显示不了星期数,是什么问题,请详细说下
时间: 2024-02-14 19:12:01 浏览: 96
Unity,Text文本滚动
这段代码中定义了一个获取当前日期和时间的类 `DateTimeText`,其中包括一个获取星期数的方法 `GetWeek()`。但是在 `Update()` 方法中,判断星期数是否变化的条件语句中使用了错误的变量名,应该是 `m_lastDateTime.DayOfWeek` 而不是 `m_lastDateTime.DayOfWeek`,导致每次更新时都无法更新星期数。
正确的条件语句应该是这样的:
```csharp
if (m_weekText != null)
{
if (m_lastDateTime.DayOfWeek != time.DayOfWeek)
{
m_weekText.text = GetWeek();
m_lastDateTime = time;
}
}
```
这样就能够正确更新星期数了。
阅读全文