This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -0,0 +1,121 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using Cysharp.Threading.Tasks;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace BITKit
{
[Serializable]
public sealed class GameTick:ITicker
{
public ulong TickCount => GameTickService.TickCount;
public void Add(Action action)=>GameTickService.Add(action);
public void Add(Action<float> action)=>GameTickService.Add(action);
public void Remove(Action<float> action)=>GameTickService.Remove(action);
}
public class GameTickService : MonoBehaviour,ITicker
{
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
_ActionQueue.Clear();
//_TickActions.Clear();
TickCount = ulong.MinValue;
}
private static readonly Queue<Action> _ActionQueue = new();
// private static readonly CacheList<Action<float>> _TickActions = new();
// public static void Add(Action<float> action) => _TickActions.Add(action);
// public static void Remove(Action<float> action) => _TickActions.Remove(action);
private static event Action<float> _TickEvents;
public static void Add( Action<float> action)=>_TickEvents += action;
public static void Remove( Action<float> action)=>_TickEvents -= action;
public static void Add(Action action)=>_ActionQueue.Enqueue(action);
public static ulong TickCount { get; private set; }
ulong ITicker.TickCount => TickCount;
void ITicker.Add(Action action)=>Add(action);
void ITicker.Add(Action<float> action)=>Add(action);
void ITicker.Remove(Action<float> action)=>Remove(action);
[SerializeField] private int tickRate = 32;
[SerializeField] private bool isMainThread;
[SerializeField] private bool isConcurrent;
private readonly Timer _timer = new();
private float _deltaTime;
private double _lastTime;
private void Start()
{
tickRate = Mathf.Clamp(tickRate, 1, 128);
_deltaTime = 1f / tickRate;
_timer.Elapsed += Tick;
_timer.AutoReset = isConcurrent;
_timer.Interval = TimeSpan.FromSeconds(_deltaTime).TotalMilliseconds;
_timer.Start();
destroyCancellationToken.Register(() =>
{
_timer.Stop();
_timer.Dispose();
});
}
private async void Tick(object sender, ElapsedEventArgs e)
{
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();
}
_TickEvents?.Invoke(delta);
// using var xEnumerator = _TickActions.GetEnumerator();
// while (xEnumerator.MoveNext())
// {
// xEnumerator.Current!.Invoke(delta);
// }
//_TickEvents?.Invoke((float)delta);
}
catch (OperationCanceledException)
{
return;
}
catch (Exception exception)
{
BIT4Log.LogException(exception);
}
Restart();
return;
void Restart()
{
if (isConcurrent is false && destroyCancellationToken.IsCancellationRequested is false)
{
_timer.Start();
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fe3489d5a0c99614fb5d0308796b4af0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,122 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Timers;
using Cysharp.Threading.Tasks;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace BITKit
{
[Serializable]
public class IntervalTick:ITicker
{
[SerializeField] private float interval;
public ulong TickCount=>IntervalTickService.GetTickCount(interval);
public void Add(Action action)
{
throw new NotImplementedException();
}
public void Add(Action<float> action)
{
IntervalTickService.Add(action, interval);
}
public void Remove(Action<float> action)
{
IntervalTickService.Remove(action, interval);
}
}
public class IntervalTickService
{
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
foreach (var x in _Timers.Values)
{
x.Stop();
x.Dispose();
}
_Timers.Clear();
_Actions.Clear();
_CreateTimes.Clear();
}
private class DelegateWrapper
{
public event Action<float> Tick;
public float Interval { get; set; }
public async void Invoke(object sender, ElapsedEventArgs args)
{
await UniTask.SwitchToMainThread();
#if UNITY_EDITOR
if (EditorApplication.isPlaying is false)
{
return;
}
#endif
try
{
Tick?.Invoke(Interval);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
}
public static void Add(Action<float> action, float interval)
{
var timer = _Timers.GetOrAdd(interval, Create);
var action1 = _Actions.GetOrAdd(interval, CreateAction);
action1.Tick += action;
}
public static void Remove(Action<float> action, float interval)
{
var timer = _Timers.GetOrAdd(interval, Create);
var action1 = _Actions.GetOrAdd(interval, CreateAction);
action1.Tick -= action;
}
public static ulong GetTickCount(float interval)
{
return GameTickService.TickCount - _CreateTimes[interval];
}
private static readonly ConcurrentDictionary<float,Timer> _Timers = new();
private static readonly ConcurrentDictionary<float,DelegateWrapper> _Actions = new();
private static readonly ConcurrentDictionary<float,ulong> _CreateTimes = new();
private static Timer Create(float interval)
{
var totalMilliseconds = TimeSpan.FromSeconds(interval).TotalMilliseconds;
var timer = new Timer()
{
Interval = totalMilliseconds,
};
_CreateTimes.TryAdd(interval, GameTickService.TickCount);
var action = _Actions.GetOrAdd(interval, CreateAction);
timer.Elapsed += action.Invoke;
timer.Start();
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()
{
Interval = interval,
};
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a8c6c9485a1d38b449eeb66b978c2fe6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: