using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UniRx;
public class DisplayTime : MonoBehaviour
{
//时间文本显示
[SerializeField]
private TextMeshProUGUI _time;
private int _timeType = 0;
enum TimeType
{
is24HourFormat,
is12HourFormat,
isError
}
private static readonly string TAG = "DisplayTime";
void Start()
{
DataCenter.TimeFormat.Subscribe(i =>
{
Log.I(TAG, "Time select value: " + i);
if (i == 0)
{
_timeType = (int)TimeType.is24HourFormat;
}
else if (i == 1)
{
_timeType = (int)TimeType.is12HourFormat;
}
else if (i == 2)
{
_timeType = (int)TimeType.isError;
}
}).AddTo(this);
}
private void Awake()
{
StartCoroutine(UpdateTimeEverySecond());
}
private void UpdateTime()
{
DateTime currentTime = DateTime.Now;
if (_timeType == (int)TimeType.is24HourFormat)
{
// 24 小时制
string timeIn24HourFormat = currentTime.ToString("HH:mm");
_time.text = timeIn24HourFormat;
}
else if (_timeType == (int)TimeType.is12HourFormat)
{
// 12 小时制
int hour = currentTime.Hour;
if (hour > 12) { hour = hour - 12; }
string timeIn12HourFormat = hour + ":" + currentTime.ToString("mm");
_time.text = timeIn12HourFormat;
}
else
{
_time.text = "--:--";
}
}
private IEnumerator UpdateTimeEverySecond()
{
while (true)
{
UpdateTime();
yield return new WaitForSeconds(1.0f);
}
}
}