This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace BITKit
{
@@ -26,6 +27,11 @@ namespace BITKit
/// </summary>
/// <param name="action"></param>
void Remove(Action<float> action);
/// <summary>
/// 手动调用循环
/// </summary>
/// <param name="delta"></param>
void ManualTick(float delta);
}
/// <summary>
/// 主线程循环
@@ -35,4 +41,40 @@ namespace BITKit
/// 线程池循环
/// </summary>
public interface IThreadTicker : ITicker { }
#if UNITY_5_3_OR_NEWER
/// <summary>
/// 最后执行的循环,通常用于旋转、位移等
/// </summary>
public interface IAfterTicker : ITicker{}
/// <summary>
/// Unity专用固定循环
/// </summary>
public interface IFixedTicker : ITicker{}
#endif
public class Ticker : ITicker
{
private readonly Queue<Action> _queue = new();
private event Action<float> TickEvents;
public ulong TickCount { get; private set; }
public void Add(Action action)
{
_queue.Enqueue(action);
}
public void Add(Action<float> action)=>TickEvents += action;
public void Remove(Action<float> action)=>TickEvents -= action;
public void ManualTick(float delta)
{
TickCount++;
while (_queue.TryDequeue(out var action))
{
action.Invoke();
}
TickEvents?.Invoke(delta);
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using BITKit;
using Timer = System.Timers.Timer;
namespace BITKit
{
public class TimerTicker:ITicker
{
public TimerTicker()
{
_timer = new Timer();
_timer.Elapsed += (sender, args) =>
{
OnTick();
if (_timer.Enabled && _timer.AutoReset is false)
{
_timer.Start();
}
};
}
public int TickRate { get; set; }
public ulong TickCount { get; private set; }
private readonly Timer _timer;
private float _deltaTime;
private event Action<float> Tick;
public void Add(Action action)
{
throw new NotImplementedException();
}
public void Add(Action<float> action)
{
Tick += action;
}
public void Remove(Action<float> action)
{
Tick -= action;
}
public void ManualTick(float delta)
{
throw new NotImplementedException();
}
public void Start()
{
_deltaTime = 1f / TickRate;
_timer.Interval = TimeSpan.FromSeconds(_deltaTime).TotalMilliseconds;
_timer.Enabled = true;
}
private void OnTick()
{
try
{
TickCount++;
Tick?.Invoke(_deltaTime);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
public void Stop()
{
_timer.Enabled = false;
}
}
}

View File

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