165 lines
4.4 KiB
C#
Raw Normal View History

2025-09-08 14:51:28 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.Networking;
public class TimeChecker : MonoBehaviour
{
// <20><><EFBFBD>õIJο<C4B2>ʱ<EFBFBD><CAB1>
public DateTime targetTime;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
public int maxRetries = 3;
// <20><>ǰ<EFBFBD><C7B0><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
private int currentRetry = 0;
public Action actionA;
public Action actionB;
public Action actionC;
/// <summary>
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䲢ִ<E4B2A2><D6B4><EFBFBD><EFBFBD>Ӧ<EFBFBD>߼<EFBFBD>
/// </summary>
public void CheckNetworkTimeAndExecute()
{
currentRetry = 0;
StartCoroutine(TryGetNetworkTime());
}
private IEnumerator TryGetNetworkTime()
{
// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
string[] timeServers = new string[]
{
"http://www.ntp.org/",
"https://www.baidu.com",
"https://www.google.com",
"https://time.is/",
"https://www.microsoft.com"
};
foreach (string server in timeServers)
{
using (UnityWebRequest www = UnityWebRequest.Get(server))
{
www.SetRequestHeader("Cache-Control", "no-cache");
www.timeout = 8;
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
string dateHeader = www.GetResponseHeader("Date");
if (!string.IsNullOrEmpty(dateHeader))
{
if (TryParseDate(dateHeader, out DateTime networkTime))
{
DateTime localTime = networkTime.ToLocalTime();
Debug.Log($"<22>ɹ<EFBFBD><C9B9><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>: {localTime:yyyy-MM-dd HH:mm:ss}");
ProcessTimeComparison(localTime);
yield break;
}
else
{
Debug.LogWarning($"<22>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʽ: {dateHeader}");
}
}
else
{
Debug.LogWarning($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {server} δ<><CEB4><EFBFBD><EFBFBD>Dateͷ<65><CDB7>Ϣ");
}
}
else
{
Debug.LogWarning($"<22><><EFBFBD><EFBFBD> {server} ʧ<><CAA7>: {www.error}");
}
}
}
// <20><><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
currentRetry++;
if (currentRetry < maxRetries)
{
Debug.Log($"<22><><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> ({currentRetry}/{maxRetries})");
yield return new WaitForSecondsRealtime(2f * currentRetry);
StartCoroutine(TryGetNetworkTime());
}
else
{
//Debug.LogError("<22><><EFBFBD>г<EFBFBD><D0B3>Զ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3>޷<EFBFBD><DEB7><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>");
// ʱ<><CAB1><EFBFBD><EFBFBD>ȡʧ<C8A1>ܣ<EFBFBD>ִ<EFBFBD><D6B4>C<EFBFBD>߼<EFBFBD>
ExecuteLogicC();
}
}
// <20><><EFBFBD>Զ<EFBFBD><D4B6>ָ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private bool TryParseDate(string dateString, out DateTime dateTime)
{
string[] formats = {
"r",
"ddd, dd MMM yyyy HH:mm:ss GMT",
"dddd, dd-MMM-yy HH:mm:ss GMT",
"yyyy-MM-dd HH:mm:ss",
"ddd MMM dd HH:mm:ss yyyy"
};
return DateTime.TryParseExact(
dateString,
formats,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
out dateTime
);
}
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ƚ<EFBFBD>
private void ProcessTimeComparison(DateTime networkTime)
{
Debug.Log($"<22><><EFBFBD>õ<EFBFBD>Ŀ<EFBFBD><C4BF>ʱ<EFBFBD><CAB1>: {targetTime:yyyy-MM-dd HH:mm:ss}");
Debug.Log($"<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>(<28><><EFBFBD><EFBFBD>): {networkTime:yyyy-MM-dd HH:mm:ss}");
if (networkTime <= targetTime)
{
ExecuteLogicA();
}
else
{
ExecuteLogicB();
}
}
/// <summary>
/// <20>߼<EFBFBD>A<EFBFBD><41><EFBFBD><EFBFBD>ǰʱ<C7B0><CAB1>С<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʱִ<CAB1><D6B4>
/// </summary>
private void ExecuteLogicA()
{
actionA?.Invoke();
}
/// <summary>
/// <20>߼<EFBFBD>B<EFBFBD><42><EFBFBD><EFBFBD>ǰʱ<C7B0><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʱִ<CAB1><D6B4>
/// </summary>
private void ExecuteLogicB()
{
actionB?.Invoke();
}
/// <summary>
/// <20>߼<EFBFBD>C<EFBFBD><43>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ȡʧ<C8A1><CAA7>ʱִ<CAB1><D6B4>
/// </summary>
private void ExecuteLogicC()
{
actionC?.Invoke();
}
// ʾ<><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private void OnEnable()
{
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>ʱ<EFBFBD><CAB1>ʾ<EFBFBD><CABE>
targetTime = new DateTime(2025,10,1);
CheckNetworkTimeAndExecute();
}
}