From 139e0d5433bf75d8e54b86b58ac62b4aedb2974a Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Wed, 25 May 2022 23:33:11 +0800
Subject: [PATCH] =?UTF-8?q?GameTime=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1?=
=?UTF-8?q?=E5=99=A8=E6=97=B6=E9=97=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
GameTime增加服务器时间
---
Assets/TEngine/Runtime/Core/GameTime.cs | 40 ++++++++++++++++++-
Assets/TEngine/Runtime/Core/TEngine.cs | 1 +
.../src/TEngineCore/Core/GameTime.cs | 40 ++++++++++++++++++-
.../src/TEngineCore/Core/TEngine.cs | 1 +
4 files changed, 78 insertions(+), 4 deletions(-)
diff --git a/Assets/TEngine/Runtime/Core/GameTime.cs b/Assets/TEngine/Runtime/Core/GameTime.cs
index 0aee6ff7..f7190a10 100644
--- a/Assets/TEngine/Runtime/Core/GameTime.cs
+++ b/Assets/TEngine/Runtime/Core/GameTime.cs
@@ -7,6 +7,14 @@ namespace TEngine
///
public static class GameTime
{
+ ///
+ /// 开始记录
+ ///
+ public static void Start()
+ {
+ _lastFrameTimeStamp = TimeStamp;
+ }
+
///
/// 这一帧的记录
///
@@ -17,14 +25,34 @@ namespace TEngine
quickRealTime = Time.realtimeSinceStartup;
frameCount = Time.frameCount;
unscaledTime = Time.unscaledTime;
+
+ long now = TimeStamp;
+
+ if (ServerTimeStamp > 0)
+ {
+ ServerTimeStamp += now - _lastFrameTimeStamp;
+ }
+ else
+ {
+ ServerTimeStamp = now;
+ }
+ _lastFrameTimeStamp = now;
}
public static float time;
public static float deltaTime;
public static int frameCount;
public static float unscaledTime;
+ public static long ServerTimeStamp;
+ private static System.DateTime _epochDateTime = System.TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), System.TimeZoneInfo.Local);
+ public static float quickRealTime;
+ public static long TimeStamp => System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
+ private static long _lastFrameTimeStamp;
- public static float realtimeSinceStartup
+ ///
+ /// 从游戏启动到现在的真实时长(秒)
+ ///
+ public static float RealtimeSinceStartup
{
get
{
@@ -32,6 +60,14 @@ namespace TEngine
}
}
- public static float quickRealTime;
+ ///
+ /// 服务器同步时间
+ ///
+ ///
+ public static void ServerTimeSync(long serverTime)
+ {
+ ServerTimeStamp = serverTime;
+ }
+
}
}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Core/TEngine.cs b/Assets/TEngine/Runtime/Core/TEngine.cs
index acd4893e..cbf33cbd 100644
--- a/Assets/TEngine/Runtime/Core/TEngine.cs
+++ b/Assets/TEngine/Runtime/Core/TEngine.cs
@@ -74,6 +74,7 @@ namespace TEngine
#region 生命周期
public void Start()
{
+ GameTime.Start();
GameTime.StartFrame();
var listLogic = m_LogicMgrList;
var logicCnt = listLogic.Count;
diff --git a/TEngineHotUpdate/src/TEngineCore/Core/GameTime.cs b/TEngineHotUpdate/src/TEngineCore/Core/GameTime.cs
index a6c158d0..7911ee21 100644
--- a/TEngineHotUpdate/src/TEngineCore/Core/GameTime.cs
+++ b/TEngineHotUpdate/src/TEngineCore/Core/GameTime.cs
@@ -7,6 +7,14 @@ namespace TEngineCore
///
public static class GameTime
{
+ ///
+ /// 开始记录
+ ///
+ public static void Start()
+ {
+ _lastFrameTimeStamp = TimeStamp;
+ }
+
///
/// 这一帧的记录
///
@@ -17,14 +25,34 @@ namespace TEngineCore
quickRealTime = Time.realtimeSinceStartup;
frameCount = Time.frameCount;
unscaledTime = Time.unscaledTime;
+
+ long now = TimeStamp;
+
+ if (ServerTimeStamp > 0)
+ {
+ ServerTimeStamp += now - _lastFrameTimeStamp;
+ }
+ else
+ {
+ ServerTimeStamp = now;
+ }
+ _lastFrameTimeStamp = now;
}
public static float time;
public static float deltaTime;
public static int frameCount;
public static float unscaledTime;
+ public static long ServerTimeStamp;
+ private static System.DateTime _epochDateTime = System.TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), System.TimeZoneInfo.Local);
+ public static float quickRealTime;
+ public static long TimeStamp => System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
+ private static long _lastFrameTimeStamp;
- public static float realtimeSinceStartup
+ ///
+ /// 从游戏启动到现在的真实时长(秒)
+ ///
+ public static float RealtimeSinceStartup
{
get
{
@@ -32,6 +60,14 @@ namespace TEngineCore
}
}
- public static float quickRealTime;
+ ///
+ /// 服务器同步时间
+ ///
+ ///
+ public static void ServerTimeSync(long serverTime)
+ {
+ ServerTimeStamp = serverTime;
+ }
+
}
}
\ No newline at end of file
diff --git a/TEngineHotUpdate/src/TEngineCore/Core/TEngine.cs b/TEngineHotUpdate/src/TEngineCore/Core/TEngine.cs
index be8545fe..95d0316b 100644
--- a/TEngineHotUpdate/src/TEngineCore/Core/TEngine.cs
+++ b/TEngineHotUpdate/src/TEngineCore/Core/TEngine.cs
@@ -74,6 +74,7 @@ namespace TEngineCore
#region 生命周期
public void Start()
{
+ GameTime.Start();
GameTime.StartFrame();
var listLogic = m_LogicMgrList;
var logicCnt = listLogic.Count;