This commit is contained in:
CortexCore
2025-03-24 14:42:40 +08:00
parent 18239a5ae4
commit 9845d20f7f
99 changed files with 5418 additions and 5512 deletions

View File

@@ -8,6 +8,7 @@ using Cysharp.Threading.Tasks;
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.LowLevel;
namespace BITKit
{
@@ -58,15 +59,18 @@ namespace BITKit
}
[SerializeField] private int tickRate = 32;
[SerializeField] private bool isMainThread;
[SerializeField] private bool isConcurrent;
[SerializeField] private string lastTickTime="0";
private readonly Timer _timer = new();
private float _deltaTime;
private double _lastTime;
private PlayerLoopSystem _playerLoop;
private int _update;
private void Start()
{
_playerLoop = PlayerLoop.GetCurrentPlayerLoop();
tickRate = Mathf.Clamp(tickRate, 1, 128);
_deltaTime = 1f / tickRate;
@@ -81,32 +85,34 @@ namespace BITKit
_timer.Dispose();
});
}
private async void Tick(object sender, ElapsedEventArgs e)
private void Update()
{
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();
}
if (EditorApplication.isPlaying is false)
{
_update = 0;
return;
}
#endif
for (var i = 0; i < _update; i++)
{
var delta = Time.deltaTime;
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);
_TickEvents?.Invoke(delta);
}
_update = 0;
}
private void Tick(object sender, ElapsedEventArgs e)
{
TickCount++;
try
{
_update++;
}
catch (OperationCanceledException)
{
@@ -126,7 +132,6 @@ _TickEvents?.Invoke(delta);
_timer.Start();
}
}
}
}
}