This commit is contained in:
2026-07-27 16:59:22 +08:00
parent 48863cc4f8
commit 738b2b2d36
11 changed files with 298 additions and 89 deletions
+15 -2
View File
@@ -60,6 +60,15 @@ public class Pool : Singleton<Pool>
{
public Dictionary<int, PoolData> dic = new Dictionary<int, PoolData>();//生成一个字典
private GameObject poolObj;//Scene窗口中的“pool”总收纳盒
/// <summary>获取/创建池总父节点(初始化和 Return 共用同一个 poolObj)</summary>
public GameObject EnsurePoolRoot()
{
if (poolObj == null)
poolObj = new GameObject("pool");
return poolObj;
}
public GameObject Get(int id, Vector3 position, Quaternion rotation)//返回一个预制体,
{
GameObject obj;
@@ -116,6 +125,7 @@ public class Pool : Singleton<Pool>
{
if(obj.activeSelf)
{
// ⭐ 统一用 poolObj 作为池总父节点(初始化时也用 poolObj,保证 Clear 能统一销毁)
if(poolObj==null)
{
poolObj = new GameObject("pool");
@@ -133,7 +143,7 @@ public class Pool : Singleton<Pool>
}
return obj;
}
//public void Clear()//切换场景时将所有缓存清除
//{
// dic.Clear();
@@ -144,9 +154,12 @@ public class Pool : Singleton<Pool>
/// </summary>
public void Clear()
{
// ⭐ 防重入:已经清空过直接返回
if (dic.Count == 0 && poolObj == null) return;
foreach (var kv in dic.Values)
{
kv.Clear();
if (kv != null) kv.Clear();
}
dic.Clear();
+27 -30
View File
@@ -82,10 +82,14 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
foreach (string url in timeServers)
{
// 记录请求开始时间,用于 RTT 补偿
DateTime requestStartTime = DateTime.Now;
using (UnityWebRequest request = UnityWebRequest.Get(url + "?_=" + DateTime.Now.Ticks))
{
request.timeout = 5;
yield return request.SendWebRequest();
// 收到响应的本地时间
DateTime requestEndTime = DateTime.Now;
if (request.result == UnityWebRequest.Result.Success)
{
@@ -101,21 +105,20 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
try
{
JObject json = JObject.Parse(jsonText);
DateTime? serverTime = null;
if (url.Contains("47.109.132.12"))
{
// 返回格式:{"data":{"dateTime":"2026-07-26 20:02:48",...},"code":0,"info":null}
// 返回格式:{"data":{"dateTime":"2026-07-26 20:02:48","milliSeconds":330,...},"code":0,"info":null}
// dateTime 只精确到秒,需要补 milliSeconds 字段
string timeStr = json["data"]?["dateTime"]?.ToString();
Debug.Log($"47.109.132.12 提取到的时间字符串:{timeStr}");
int ms = json["data"]?["milliSeconds"]?.Value<int>() ?? 0;
Debug.Log($"47.109.132.12 提取到的时间字符串:{timeStr} 毫秒:{ms}");
if (!string.IsNullOrEmpty(timeStr) &&
DateTime.TryParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out DateTime serverTime))
DateTime.TryParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null, System.Globalization.DateTimeStyles.None, out DateTime t))
{
_serverTimeOffset = serverTime - DateTime.Now;
_isTimeSynced = true;
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
OnTimeSynced?.Invoke(CurrentBeijingTime);
yield break;
serverTime = t.AddMilliseconds(ms);
}
else
{
@@ -124,52 +127,46 @@ public class BeijingTimeManager : MonoSingleton<BeijingTimeManager>
}
else if (url.Contains("timeapi.io"))
{
string timeStr = json["dateTime"]?.ToString();
Debug.Log(timeStr);
if (!string.IsNullOrEmpty(timeStr))
{
DateTime serverTime = DateTime.Parse(timeStr);
_serverTimeOffset = serverTime - DateTime.Now;
_isTimeSynced = true;
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
OnTimeSynced?.Invoke(CurrentBeijingTime);
yield break;
serverTime = DateTime.Parse(timeStr);
}
}
else if (url.Contains("worldtimeapi"))
{
string timeStr = json["datetime"]?.ToString();
Debug.Log(timeStr);
if (!string.IsNullOrEmpty(timeStr))
{
DateTime serverTime = DateTimeOffset.Parse(timeStr)
serverTime = DateTimeOffset.Parse(timeStr)
.ToOffset(TimeSpan.FromHours(8))
.DateTime;
_serverTimeOffset = serverTime - DateTime.Now;
_isTimeSynced = true;
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
OnTimeSynced?.Invoke(CurrentBeijingTime);
yield break;
}
}
else if (url.Contains("suning"))
{
string timeStr = json["sysTime2"]?.ToString();
Debug.Log(timeStr);
if (!string.IsNullOrEmpty(timeStr))
{
DateTime serverTime = DateTime.ParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null);
_serverTimeOffset = serverTime - DateTime.Now;
_isTimeSynced = true;
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss},偏移量:{_serverTimeOffset.TotalSeconds:F1}s");
OnTimeSynced?.Invoke(CurrentBeijingTime);
yield break;
serverTime = DateTime.ParseExact(timeStr, "yyyy-MM-dd HH:mm:ss", null);
}
}
if (serverTime.HasValue)
{
// RTT 补偿:服务器生成响应到客户端接收耗时约为 RTT/2(假设网络对称)
// 补偿后服务器时间 = 服务器响应时间 + RTT/2
TimeSpan rtt = requestEndTime - requestStartTime;
DateTime compensatedServerTime = serverTime.Value + TimeSpan.FromTicks(rtt.Ticks / 2);
_serverTimeOffset = compensatedServerTime - requestEndTime;
_isTimeSynced = true;
Debug.Log($"北京时间同步成功:{CurrentBeijingTime:yyyy-MM-dd HH:mm:ss.fff},偏移量:{_serverTimeOffset.TotalSeconds:F3}s,RTT:{rtt.TotalSeconds:F3}s");
OnTimeSynced?.Invoke(CurrentBeijingTime);
yield break;
}
}
catch (Exception e)
{