Files

106 lines
4.6 KiB
C#

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.InputSystem;
// Raw Input 除錯工具 (開發用,不要放進正式場景)
// 掛在任何場景的任一物件上 : 畫面左下角顯示統計,按 F9 把最近 10 秒的原始事件存到桌面
public class RawInputProbe : MonoBehaviour
{
[Tooltip("按 F9 存檔時保留最近幾秒")]
public float keepSeconds = 10f;
private readonly List<RawMouseInput.Event> frameEvents = new List<RawMouseInput.Event>();
private readonly List<RawMouseInput.Event> history = new List<RawMouseInput.Event>();
private readonly List<double> gaps = new List<double>();
private int rawPerSecond;
private int rawThisSecond;
private float secondTimer;
private double lastT;
private string gapText = "";
private int unityDeltaFrames; // 這一秒內 Unity 自己的 Mouse.delta 有動的幀數
private int unityDeltaFramesShown;
private Vector2 rawSumThisFrame;
private Vector2 unityDeltaThisFrame;
private string saveMessage = "";
void Update()
{
frameEvents.Clear();
frameEvents.AddRange(RawMouseInput.FrameEvents);
rawSumThisFrame = Vector2.zero;
foreach (RawMouseInput.Event e in frameEvents)
{
rawSumThisFrame += new Vector2(e.dx, e.dy);
if (lastT > 0) gaps.Add(e.t - lastT);
lastT = e.t;
history.Add(e);
}
rawThisSecond += frameEvents.Count;
// Unity 自己還收不收得到滑鼠位移 (用來確認 Raw Input 接管後的影響)
unityDeltaThisFrame = Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
if (unityDeltaThisFrame != Vector2.zero) unityDeltaFrames++;
double cut = Time.realtimeSinceStartupAsDouble - keepSeconds;
int remove = 0;
while (remove < history.Count && history[remove].t < cut) remove++;
if (remove > 0) history.RemoveRange(0, remove);
secondTimer += Time.unscaledDeltaTime;
if (secondTimer >= 1f)
{
rawPerSecond = rawThisSecond;
unityDeltaFramesShown = unityDeltaFrames;
gapText = DescribeGaps();
rawThisSecond = 0;
unityDeltaFrames = 0;
gaps.Clear();
secondTimer = 0f;
}
if (Keyboard.current != null && Keyboard.current.f9Key.wasPressedThisFrame) SaveHistory();
}
// 這一秒內事件間隔的分布 : 真的逐筆的話,間隔會集中在 1/回報率 附近,幾乎沒有 0
string DescribeGaps()
{
if (gaps.Count < 2) return "(移動滑鼠看看)";
gaps.Sort();
int zero = 0;
foreach (double g in gaps) if (g < 0.00001) zero++;
double median = gaps[gaps.Count / 2] * 1000.0;
double p10 = gaps[gaps.Count / 10] * 1000.0;
double p90 = gaps[gaps.Count * 9 / 10] * 1000.0;
return $"間隔中位數 {median:F3}ms (10%~90% : {p10:F3}~{p90:F3}ms),幾乎同時 (<0.01ms) 的比例 {100f * zero / gaps.Count:F0}%";
}
void SaveHistory()
{
StringBuilder sb = new StringBuilder("t,dx,dy,left\n");
foreach (RawMouseInput.Event e in history)
{
sb.Append(e.t.ToString("F6", CultureInfo.InvariantCulture)).Append(',')
.Append(e.dx).Append(',').Append(e.dy).Append(',').Append(e.left ? 1 : 0).Append('\n');
}
string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "raw_probe.csv");
File.WriteAllText(path, sb.ToString());
saveMessage = $"已存 {history.Count} 筆到 {path}";
}
void OnGUI()
{
float y = Screen.height - 120;
GUI.Box(new Rect(10, y - 5, 760, 120), "");
GUI.Label(new Rect(20, y, 740, 22), $"Raw Input : {RawMouseInput.Status} 左鍵 {(RawMouseInput.LeftDown ? "按著" : "放開")} 重新註冊 {RawMouseInput.Reregistrations} 次");
GUI.Label(new Rect(20, y + 20, 740, 22), $"每秒 {rawPerSecond} 筆 {gapText}");
GUI.Label(new Rect(20, y + 40, 740, 22), $"這一幀 Raw 總和 ({rawSumThisFrame.x:F0}, {rawSumThisFrame.y:F0}) Unity Mouse.delta ({unityDeltaThisFrame.x:F0}, {unityDeltaThisFrame.y:F0}) Unity 有動的幀數 {unityDeltaFramesShown}/秒");
GUI.Label(new Rect(20, y + 60, 740, 22), $"略過 : 視窗不在前景 {RawMouseInput.IgnoredUnfocused} 筆、絕對座標 {RawMouseInput.IgnoredAbsolute} 筆");
GUI.Label(new Rect(20, y + 80, 740, 22), string.IsNullOrEmpty(saveMessage) ? "按 F9 存最近 10 秒到桌面 raw_probe.csv" : saveMessage);
}
}