1
This commit is contained in:
@@ -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