123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using Cysharp.Threading.Tasks.Triggers;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Mathematics;
- using UnityEngine;
- public class TimeCounting : MonoBehaviour
- {
- public List<GameObject> numPrefabs;
- public List<GameObject> tempNum;
- public List<Transform> pos;
- public GameObject door;
- private long finishTime;
- private long nowTime;
- private double sum;
- private int hours;
- private int minutes;
- private int seconds;
- private float interval = 1;
- private float intervalTimer = 1;
- private void Start()
- {
- //TimeSpan st = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);//获取时间戳
- //nowTime = (long)st.TotalMilliseconds;
- //sum = finishTime - nowTime;
- Debug.Log(StepManager.timeStamp);
- finishTime = long.Parse(StepManager.timeStamp);
- //finishTime = 1709168400;
- nowTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
- sum = finishTime - nowTime;
- }
- private void Update()
- {
- Countdown();
- intervalTimer += Time.deltaTime;
- if(intervalTimer >= interval) InitNumModels();
- //if (Input.GetKeyDown(KeyCode.T))
- //{
- // Time.timeScale = 0;
- //}
- //if (Input.GetKeyDown(KeyCode.Y))
- //{
- // Time.timeScale = 1;
- //}
- }
- /// <summary>
- /// 获取倒计时总时长
- /// </summary>
- private void Countdown()
- {
- sum -= (double)Time.deltaTime;
- if (sum <= 0)
- {
- door.SetActive(false);
- gameObject.SetActive(false);
- return;
- }
- DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds((long)sum).DateTime;
- int day = dateTime.Day;
- int month = dateTime.Month;
- hours = dateTime.Hour + (day - 1) * 24 + (month-1)* 30 * 24;
- minutes = dateTime.Minute;
- seconds = dateTime.Second;
- //Debug.Log(day+" //"+hours + "/" + minutes + "/" + seconds);
- }
- private void InitNumModels()
- {
- //清理已生成的数字模型
- for (int i = 0; i < tempNum.Count; i++)
- {
- Destroy(tempNum[i]);
- }
- tempNum.Clear();
- //放置和更新数字模型
- PlaceAndShowNumberPrefabs((hours / 100) % 10, 0);
- PlaceAndShowNumberPrefabs((hours / 10) % 10, 1);
- PlaceAndShowNumberPrefabs((hours % 10), 2);
- PlaceAndShowNumberPrefabs((minutes / 10) % 10, 3);
- PlaceAndShowNumberPrefabs((minutes % 10), 4);
- PlaceAndShowNumberPrefabs((seconds / 10) % 10, 5);
- PlaceAndShowNumberPrefabs((seconds % 10), 6);
- intervalTimer = 0;
- }
- private void PlaceAndShowNumberPrefabs(int num,int posIndex)
- {
- GameObject objOfNum = Instantiate(numPrefabs[num]);
- objOfNum.transform.position = pos[posIndex].position;
- objOfNum.transform.SetParent(this.transform);
- objOfNum.transform.localEulerAngles = Vector3.zero;
- objOfNum.transform.localScale = new Vector3(1, 1, 1);
- objOfNum.SetActive(true);
- tempNum.Add(objOfNum);
- }
- }
|