1
This commit is contained in:
@@ -3,6 +3,9 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
@@ -71,9 +74,16 @@ namespace BITKit
|
||||
TickCount++;
|
||||
try
|
||||
{
|
||||
|
||||
var delta = (float)(BITApp.Time.TimeAsDouble - _lastTime);
|
||||
_lastTime = BITApp.Time.TimeAsDouble;
|
||||
if (isMainThread) await UniTask.SwitchToMainThread(destroyCancellationToken);
|
||||
#if UNITY_EDITOR
|
||||
if (EditorApplication.isPlaying is false)
|
||||
{
|
||||
Restart();
|
||||
}
|
||||
#endif
|
||||
while (_ActionQueue.TryDequeue(out var action))
|
||||
{
|
||||
action?.Invoke();
|
||||
@@ -95,10 +105,16 @@ _TickEvents?.Invoke(delta);
|
||||
BIT4Log.LogException(exception);
|
||||
}
|
||||
|
||||
if (isConcurrent is false && destroyCancellationToken.IsCancellationRequested is false)
|
||||
Restart();
|
||||
return;
|
||||
void Restart()
|
||||
{
|
||||
_timer.Start();
|
||||
if (isConcurrent is false && destroyCancellationToken.IsCancellationRequested is false)
|
||||
{
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,9 @@ using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
@@ -48,11 +51,23 @@ namespace BITKit
|
||||
{
|
||||
public event Action<float> Tick;
|
||||
public float Interval { get; set; }
|
||||
|
||||
public async void Invoke(object sender, ElapsedEventArgs args)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
Tick?.Invoke(Interval);
|
||||
#if UNITY_EDITOR
|
||||
if (EditorApplication.isPlaying is false || EditorApplication.isPaused)
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
try
|
||||
{
|
||||
Tick?.Invoke(Interval);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void Add(Action<float> action, float interval)
|
||||
@@ -81,7 +96,6 @@ namespace BITKit
|
||||
{
|
||||
Interval = totalMilliseconds,
|
||||
};
|
||||
timer.AutoReset = true;
|
||||
_CreateTimes.TryAdd(interval, GameTickService.TickCount);
|
||||
|
||||
var action = _Actions.GetOrAdd(interval, CreateAction);
|
||||
@@ -91,6 +105,13 @@ namespace BITKit
|
||||
|
||||
BIT4Log.Log<IntervalTickService>($"已创建Tick,Interval[{totalMilliseconds}]");
|
||||
return timer;
|
||||
|
||||
void Tick(object sender, ElapsedEventArgs args)
|
||||
{
|
||||
action.Invoke(sender, args);
|
||||
if (timer.Enabled)
|
||||
timer.Start();
|
||||
}
|
||||
}
|
||||
private static DelegateWrapper CreateAction(float interval)=>new()
|
||||
{
|
||||
|
Reference in New Issue
Block a user