TimeCounting.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  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. Debug.Log(StepManager.timeStamp);
  27. finishTime = long.Parse(StepManager.timeStamp);
  28. //finishTime = 1709168400;
  29. nowTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  30. sum = finishTime - nowTime;
  31. }
  32. private void Update()
  33. {
  34. Countdown();
  35. intervalTimer += Time.deltaTime;
  36. if(intervalTimer >= interval) InitNumModels();
  37. //if (Input.GetKeyDown(KeyCode.T))
  38. //{
  39. // Time.timeScale = 0;
  40. //}
  41. //if (Input.GetKeyDown(KeyCode.Y))
  42. //{
  43. // Time.timeScale = 1;
  44. //}
  45. }
  46. /// <summary>
  47. /// 获取倒计时总时长
  48. /// </summary>
  49. private void Countdown()
  50. {
  51. sum -= (double)Time.deltaTime;
  52. if (sum <= 0)
  53. {
  54. door.SetActive(false);
  55. gameObject.SetActive(false);
  56. return;
  57. }
  58. DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds((long)sum).DateTime;
  59. int day = dateTime.Day;
  60. int month = dateTime.Month;
  61. hours = dateTime.Hour + (day - 1) * 24 + (month-1)* 30 * 24;
  62. minutes = dateTime.Minute;
  63. seconds = dateTime.Second;
  64. //Debug.Log(day+" //"+hours + "/" + minutes + "/" + seconds);
  65. }
  66. private void InitNumModels()
  67. {
  68. //清理已生成的数字模型
  69. for (int i = 0; i < tempNum.Count; i++)
  70. {
  71. Destroy(tempNum[i]);
  72. }
  73. tempNum.Clear();
  74. //放置和更新数字模型
  75. PlaceAndShowNumberPrefabs((hours / 100) % 10, 0);
  76. PlaceAndShowNumberPrefabs((hours / 10) % 10, 1);
  77. PlaceAndShowNumberPrefabs((hours % 10), 2);
  78. PlaceAndShowNumberPrefabs((minutes / 10) % 10, 3);
  79. PlaceAndShowNumberPrefabs((minutes % 10), 4);
  80. PlaceAndShowNumberPrefabs((seconds / 10) % 10, 5);
  81. PlaceAndShowNumberPrefabs((seconds % 10), 6);
  82. intervalTimer = 0;
  83. }
  84. private void PlaceAndShowNumberPrefabs(int num,int posIndex)
  85. {
  86. GameObject objOfNum = Instantiate(numPrefabs[num]);
  87. objOfNum.transform.position = pos[posIndex].position;
  88. objOfNum.transform.SetParent(this.transform);
  89. objOfNum.transform.localEulerAngles = Vector3.zero;
  90. objOfNum.transform.localScale = new Vector3(1, 1, 1);
  91. objOfNum.SetActive(true);
  92. tempNum.Add(objOfNum);
  93. }
  94. }