TimeCounting.cs 2.7 KB

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