startUI告一段落
This commit is contained in:
1 parent
f8154f92b3
commit
da28b62f7f
34 files changed
+17562
-8231
No files matched your search
@@ -32,6 +32,15 @@ public class Polling : MonoBehaviour
|
||||
private float timeSinceLastEvent = 0f;
|
||||
private const float idleTimeout = 2f; // 超過 2 秒沒動就歸零
|
||||
|
||||
[Header("顯示")]
|
||||
[Tooltip("文字更新間隔 (秒),偵測仍然每幀進行")]
|
||||
public float updateInterval = 0.5f;
|
||||
private float displayTimer = 0f;
|
||||
private int shownHz = -1; // 上次顯示的數值,沒變就不重設文字
|
||||
|
||||
// 與 hook 交換用的暫存 List,避免每幀 new 一個新的
|
||||
private List<double> currentBatch = new List<double>();
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
stopwatch.Start();
|
||||
@@ -77,11 +86,13 @@ public class Polling : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
List<double> currentBatch;
|
||||
// 把 hook 收集的資料換過來,hook 改寫進清空的那一份
|
||||
currentBatch.Clear();
|
||||
lock (lockObj)
|
||||
{
|
||||
currentBatch = new List<double>(intervals);
|
||||
intervals.Clear();
|
||||
List<double> temp = intervals;
|
||||
intervals = currentBatch;
|
||||
currentBatch = temp;
|
||||
}
|
||||
|
||||
if (currentBatch.Count > 0)
|
||||
@@ -103,9 +114,8 @@ public class Polling : MonoBehaviour
|
||||
|
||||
if (smoothedMs > 0)
|
||||
{
|
||||
double rawHz = 1000.0 / smoothedMs;
|
||||
int standardHz = SnapToStandardPollingRate(rawHz);
|
||||
recentRates.Add(standardHz);
|
||||
// 直接用實測值,不對齊到 250 / 500 / 1000... 等標準值
|
||||
recentRates.Add((int)Math.Round(1000.0 / smoothedMs));
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -119,48 +129,30 @@ public class Polling : MonoBehaviour
|
||||
smoothedMs = 0;
|
||||
}
|
||||
|
||||
// 更新 UI
|
||||
if (pollingDisplay != null)
|
||||
int maxHz = 0;
|
||||
if (recentRates.Count > 0)
|
||||
{
|
||||
if (recentRates.Count > 0)
|
||||
foreach (int rate in recentRates)
|
||||
{
|
||||
int maxHz = recentRates[0];
|
||||
foreach (int rate in recentRates)
|
||||
{
|
||||
if (rate >= maxHz) maxHz = rate;
|
||||
}
|
||||
|
||||
pollingDisplay.text = $"滑鼠回報率: {maxHz} Hz";
|
||||
|
||||
if (recentRates.Count > 60)
|
||||
{
|
||||
recentRates.RemoveAt(0);
|
||||
}
|
||||
if (rate >= maxHz) maxHz = rate;
|
||||
}
|
||||
else
|
||||
{
|
||||
pollingDisplay.text = "滑鼠回報率: 0 Hz";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int SnapToStandardPollingRate(double rawHz)
|
||||
{
|
||||
int[] standardRates = {63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000};
|
||||
int closestRate = standardRates[0];
|
||||
double minDifference = double.MaxValue;
|
||||
|
||||
foreach (int rate in standardRates)
|
||||
{
|
||||
double diff = Math.Abs(rawHz - rate);
|
||||
if (diff < minDifference)
|
||||
if (recentRates.Count > 60)
|
||||
{
|
||||
minDifference = diff;
|
||||
closestRate = rate;
|
||||
recentRates.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
return closestRate;
|
||||
// 每 updateInterval 秒才更新一次文字,數值沒變就不重設
|
||||
displayTimer += Time.unscaledDeltaTime;
|
||||
if (displayTimer < updateInterval) return;
|
||||
displayTimer = 0f;
|
||||
|
||||
if (pollingDisplay != null && maxHz != shownHz)
|
||||
{
|
||||
pollingDisplay.text = $"滑鼠回報率: {maxHz} Hz";
|
||||
shownHz = maxHz;
|
||||
}
|
||||
}
|
||||
|
||||
#region Win32 API 匯入
|
||||
|
||||
Reference in new issue
Block a user