Skip to content

Commit 349ec93

Browse files
author
zhangyang
committed
增加Timer
1 parent 3f26df3 commit 349ec93

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

Base/GameMode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public partial class GameMode : MonoBehaviour
3333
public static PoolManager Pool;
3434
public static DebuggerManager Debugger;
3535
public static ConfigManager Config;
36+
public static TimerManager Timer;
3637
public static GameMode Self;
3738

3839

@@ -108,6 +109,7 @@ IEnumerator Start()
108109
Network = GameFrameworkMode.GetModule<NetworkManager>();
109110
Pool = GameFrameworkMode.GetModule<PoolManager>();
110111
Debugger = GameFrameworkMode.GetModule<DebuggerManager>();
112+
Timer = GameFrameworkMode.GetModule<TimerManager>();
111113
#endregion
112114

113115
#region resource

GameFramework/Runtime/Timer.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GameFramework/Runtime/Timer/Timer.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace Wanderer.GameFramework
7+
{
8+
public class Timer : MonoBehaviour
9+
{
10+
float _realtimeSinceStartup;
11+
float _liveTime;
12+
private Action _onInvokeCallback;
13+
public void SetStartTime()
14+
{
15+
_realtimeSinceStartup = Time.realtimeSinceStartup;
16+
}
17+
18+
public float GetLiveTime()
19+
{
20+
_liveTime = Time.realtimeSinceStartup - _realtimeSinceStartup;
21+
return _liveTime;
22+
}
23+
24+
public void RunAction(float interval,Action onInvokeCallback, float repeatRate=0.0f)
25+
{
26+
_onInvokeCallback = onInvokeCallback;
27+
if (repeatRate > 0)
28+
{
29+
InvokeRepeating("InvokeAction", interval, repeatRate);
30+
}
31+
else
32+
{
33+
Invoke("InvokeAction",interval);
34+
}
35+
}
36+
37+
38+
private void InvokeAction()
39+
{
40+
_onInvokeCallback?.Invoke();
41+
}
42+
}
43+
}

GameFramework/Runtime/Timer/Timer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Wanderer.GameFramework
6+
{
7+
public class TimerManager : GameFrameworkModule
8+
{
9+
private List<Timer> _timers = new List<Timer>();
10+
11+
public Timer Start()
12+
{
13+
var timerGo = new GameObject();
14+
timerGo.hideFlags = HideFlags.HideAndDontSave;
15+
UnityEngine.Object.DontDestroyOnLoad(timerGo);
16+
Timer timer = timerGo.AddComponent<Timer>();
17+
timer.SetStartTime();
18+
_timers.Add(timer);
19+
return timer;
20+
}
21+
22+
public float Stop(Timer timer)
23+
{
24+
float time = -1;
25+
if (timer != null&& timer.gameObject!=null)
26+
{
27+
time = timer.GetLiveTime();
28+
GameObject.Destroy(timer.gameObject);
29+
}
30+
return time;
31+
}
32+
33+
public override void OnClose()
34+
{
35+
}
36+
}
37+
}

GameFramework/Runtime/Timer/TimerManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Wanderer.GameFramework
6+
{
7+
public static class GameObjectExtensions
8+
{
9+
public static T GetOrAddComponet<T>(this GameObject gameObject) where T:Component
10+
{
11+
T t = gameObject.GetComponent<T>();
12+
if (t == null)
13+
{
14+
t = gameObject.AddComponent<T>();
15+
}
16+
return t;
17+
}
18+
}
19+
}

GameFramework/Runtime/Utility/GameObjectExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)