TimeCounting.cs 3.1 KB

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