Added UI Toolkit Progress
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
@@ -34,6 +39,16 @@ namespace BITKit
|
||||
void ManualTick(float delta);
|
||||
}
|
||||
/// <summary>
|
||||
/// 异步循环
|
||||
/// </summary>
|
||||
public interface IAsyncTicker
|
||||
{
|
||||
ulong TickCount { get; }
|
||||
int TickRate { get; set; }
|
||||
bool IsConcurrent { get; set; }
|
||||
event Func<float, UniTask> OnTickAsync;
|
||||
}
|
||||
/// <summary>
|
||||
/// 主线程循环
|
||||
/// </summary>
|
||||
public interface IMainTicker : ITicker { }
|
||||
@@ -53,7 +68,106 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface IFixedTicker : ITicker{}
|
||||
#endif
|
||||
|
||||
public class AsyncTicker : IAsyncTicker,IDisposable
|
||||
{
|
||||
private readonly ILogger<AsyncTicker> _logger;
|
||||
private readonly Timer _timer=new();
|
||||
private bool _isDisposed;
|
||||
private readonly Stopwatch _stopwatch=new();
|
||||
|
||||
public AsyncTicker(ILogger<AsyncTicker> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
if (TickRate <= 0)
|
||||
{
|
||||
TickRate = 1;
|
||||
}
|
||||
|
||||
_timer.Elapsed += OnElapsed;
|
||||
|
||||
if (_isDisposed is false)
|
||||
_timer.Start();
|
||||
|
||||
_logger.LogInformation($"异步循环就绪,TickRate:{TickRate}");
|
||||
}
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
[UnityEngine.HideInCallstack]
|
||||
#endif
|
||||
private async void OnElapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(_isDisposed)return;
|
||||
|
||||
if (IsSyncContext)
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
if(_isDisposed)return;
|
||||
}
|
||||
|
||||
var deltaTime = 1f / TickRate;
|
||||
if (_stopwatch.IsRunning)
|
||||
{
|
||||
deltaTime = (float)_stopwatch.Elapsed.TotalSeconds;
|
||||
_stopwatch.Reset();
|
||||
}
|
||||
|
||||
_stopwatch.Start();
|
||||
|
||||
if (IsConcurrent)
|
||||
{
|
||||
var tasks = OnTickAsync.CastAsFunc().ToArray();
|
||||
|
||||
foreach (var func in tasks)
|
||||
{
|
||||
if (_isDisposed) return;
|
||||
|
||||
try
|
||||
{
|
||||
await func.Invoke(deltaTime);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogCritical(exception,exception.Message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
await OnTickAsync.UniTaskFunc(deltaTime);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogCritical(exception,exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogCritical(exception,exception.Message);
|
||||
}
|
||||
TickCount++;
|
||||
|
||||
if(_isDisposed)return;
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
public bool IsSyncContext { get; set; } = true;
|
||||
public ulong TickCount { get; set; }
|
||||
public int TickRate { get; set; }
|
||||
public bool IsConcurrent { get; set; }
|
||||
public event Func<float, UniTask> OnTickAsync;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class Ticker : ITicker
|
||||
{
|
||||
|
Reference in New Issue
Block a user