TimeCounting.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Cysharp.Threading.Tasks.Triggers;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. public class TimeCounting : MonoBehaviour
  8. {
  9. public List<GameObject> numPrefabs;
  10. public List<GameObject> tempNum;
  11. public List<Transform> pos;
  12. public GameObject door;
  13. private long finishTime = 1705644720;
  14. private long nowTime;
  15. private double sum;
  16. private int hours;
  17. private int minutes;
  18. private int seconds;
  19. private float interval = 1;
  20. private float intervalTimer = 1;
  21. private void Start()
  22. {
  23. //TimeSpan st = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);//获取时间戳
  24. //nowTime = (long)st.TotalMilliseconds;
  25. //sum = finishTime - nowTime;
  26. nowTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  27. sum = finishTime - nowTime;
  28. }
  29. private void Update()
  30. {
  31. Countdown();
  32. intervalTimer += Time.deltaTime;
  33. if(intervalTimer >= interval) InitNumModels();
  34. //if (Input.GetKeyDown(KeyCode.T))
  35. //{
  36. // Time.timeScale = 0;
  37. //}
  38. //if (Input.GetKeyDown(KeyCode.Y))
  39. //{
  40. // Time.timeScale = 1;
  41. //}
  42. }
  43. /// <summary>
  44. /// 获取倒计时总时长
  45. /// </summary>
  46. private void Countdown()
  47. {
  48. sum -= (double)Time.deltaTime;
  49. if (sum <= 0)
  50. {
  51. door.SetActive(false);
  52. gameObject.SetActive(false);
  53. return;
  54. }
  55. DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds((long)sum).DateTime;
  56. int day = dateTime.Day;
  57. hours = dateTime.Hour + (day - 1) * 24;
  58. minutes = dateTime.Minute;
  59. seconds = dateTime.Second;
  60. //Debug.Log(day+" //"+hours + "/" + minutes + "/" + seconds);
  61. }
  62. private void InitNumModels()
  63. {
  64. //清理已生成的数字模型
  65. for (int i = 0; i < tempNum.Count; i++)
  66. {
  67. Destroy(tempNum[i]);
  68. }
  69. tempNum.Clear();
  70. //放置和更新数字模型
  71. PlaceAndShowNumberPrefabs((hours / 100) % 10, 0);
  72. PlaceAndShowNumberPrefabs((hours / 10) % 10, 1);
  73. PlaceAndShowNumberPrefabs((hours % 10), 2);
  74. PlaceAndShowNumberPrefabs((minutes / 10) % 10, 3);
  75. PlaceAndShowNumberPrefabs((minutes % 10), 4);
  76. PlaceAndShowNumberPrefabs((seconds / 10) % 10, 5);
  77. PlaceAndShowNumberPrefabs((seconds % 10), 6);
  78. intervalTimer = 0;
  79. }
  80. private void PlaceAndShowNumberPrefabs(int num,int posIndex)
  81. {
  82. GameObject objOfNum = Instantiate(numPrefabs[num]);
  83. objOfNum.transform.position = pos[posIndex].position;
  84. objOfNum.transform.SetParent(this.transform);
  85. objOfNum.transform.localEulerAngles = Vector3.zero;
  86. objOfNum.transform.localScale = new Vector3(1, 1, 1);
  87. objOfNum.SetActive(true);
  88. tempNum.Add(objOfNum);
  89. }
  90. }