BITKit/Src/Unity/Scripts/UX/Components/UXDataComponents/UXDateTime.cs

44 lines
1.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXDateTime : MonoBehaviour
{
[SerializeReference,SubclassSelector]private IReference timeFormat;
[SerializeReference, SubclassSelector] private IReference path;
[SerializeField] private bool isTimerNotClock;
private DateTime _dateTime;
private DateTime _startTime;
private Label timeLabel;
private string lastTime;
private void Start()
{
timeLabel = GetComponent<UIDocument>().rootVisualElement.Q<Label>(path.Value);
_startTime = DateTime.Now;
}
private void Update()
{
var time = DateTime.Now;
if (isTimerNotClock)
{
time = new DateTime(DateTime.Now.Ticks - _startTime.Ticks);
}
var str = GetTime();
if(Equals(str,lastTime)) return;
lastTime = str;
timeLabel.text = lastTime;
return;
string GetTime() => timeFormat is not null
? time.ToString(timeFormat.Value)
: time.ToString(CultureInfo.InvariantCulture);
}
}
}