39 lines
1.1 KiB
C#
Raw Normal View History

using UnityEngine;
using TMPro;
public class DigitalWatch : MonoBehaviour
{
public TextMeshProUGUI textMeshPro;
void Start()
{
// Ensure the TextMeshPro component is assigned
if (textMeshPro == null)
{
textMeshPro = GetComponent<TextMeshProUGUI>();
if (textMeshPro == null)
{
Debug.LogError("TextMeshPro component not found!");
return;
}
}
// Start the update loop for the digital watch
InvokeRepeating("UpdateTime", 0f, 1f); // Update every second
}
void UpdateTime()
{
// Get the current system time
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
// Format the time to display without leading zero for single-digit hours
string formattedHour = (hour % 12).ToString(); // Assuming a 12-hour format
string formattedMinute = minute.ToString("00");
// Update the TextMeshPro text to display the time
textMeshPro.text = $"{formattedHour}:{formattedMinute}";
}
}