SetDateTime.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace Devdog.SciFiDesign.UI
  5. {
  6. public class SetDateTime : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private Text _textTimeHour;
  10. [SerializeField]
  11. private Text _textTimeMinute;
  12. [SerializeField]
  13. private Text _textColon;
  14. [SerializeField]
  15. private Text _textDay;
  16. public bool blinkColon = false;
  17. public float blinkColonInterval = 0.5f;
  18. protected void Start()
  19. {
  20. InvokeRepeating("UpdateTime", 0f, 60f);
  21. if (blinkColon)
  22. {
  23. InvokeRepeating("UpdateColon", 0f, blinkColonInterval);
  24. }
  25. }
  26. protected void UpdateTime()
  27. {
  28. var n = System.DateTime.Now;
  29. if (_textTimeHour != null)
  30. {
  31. _textTimeHour.text = n.Hour < 10 ? "0" + n.Hour : n.Hour.ToString();
  32. }
  33. if (_textTimeHour != null)
  34. {
  35. _textTimeMinute.text = n.Minute < 10 ? "0" + n.Minute : n.Minute.ToString();
  36. }
  37. if (_textDay != null)
  38. {
  39. _textDay.text = n.DayOfWeek.ToString().ToUpper();
  40. }
  41. }
  42. protected void UpdateColon()
  43. {
  44. if (string.IsNullOrEmpty(_textColon.text))
  45. {
  46. _textColon.text = ":";
  47. }
  48. else
  49. {
  50. _textColon.text = "";
  51. }
  52. }
  53. }
  54. }