TimeCounting.cs 2.9 KB

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