1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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;
- private long finishTime = 1705741200;
- 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;
- 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;
- DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds((long)sum).DateTime;
- int day = dateTime.Day;
- hours = dateTime.Hour + (day - 1) * 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);
- }
- }
|