1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user