書審261027

This commit is contained in:
Sahik committed 2026-10-27 23:50:26 +08:00
1 parent 48290de35e
commit 2d36e12f64
13 files changed
+103 -64

No files matched your search

+31 -14
View File
@@ -7,17 +7,19 @@ using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
// Unity 內建輸入 vs Raw Input 對照工具 (開發用,不要放進正式場景)
// 同一段滑鼠移動,同時用兩條路徑收事件 : Unity Input System 的逐筆事件 (eventPtr.time) 與 RawMouseInput (QPC 時間戳)
// 掛在 Test 場景的任一物件上,移動滑鼠幾秒後按 F10,桌面會出現 unity_probe.csv 與 raw_probe.csv,給 Tools/plot_input_histogram.py 畫圖 3.4
// Windows 一個程式只能有一個視窗接收滑鼠的 Raw Input : RawMouseInput 執行時,Unity Input System 收不到滑鼠事件
// 所以兩邊要輪流錄 : F11 切換模式 (Raw Input ↔ Unity),各自移動滑鼠約 10 秒,F10 把兩邊錄到的存到桌面
// 存出的 unity_probe.csv / raw_probe.csv 給 Tools/plot_input_histogram.py 畫圖 3.4
public class InputCompareProbe : MonoBehaviour
{
[Tooltip("按 F10 存檔時保留最近幾秒")]
[Tooltip("每個模式最多保留最近幾秒")]
public float keepSeconds = 10f;
private struct Ev { public double t; public float dx, dy; }
private readonly List<Ev> unityHistory = new List<Ev>();
private readonly List<Ev> rawHistory = new List<Ev>();
private bool unityMode; // false = 錄 Raw Input、true = 停掉 Raw Input 錄 Unity
private int unityThisSecond, rawThisSecond, unityPerSecond, rawPerSecond;
private float secondTimer;
private string saveMessage = "";
@@ -32,10 +34,12 @@ public class InputCompareProbe : MonoBehaviour
void OnDisable()
{
InputSystem.onEvent -= OnInputEvent;
if (unityMode) RawMouseInput.Begin(); // 離開時把 Raw Input 開回來
}
void OnInputEvent(InputEventPtr eventPtr, InputDevice device)
{
if (!unityMode) return;
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>()) return;
if (!(device is Mouse mouse)) return;
if (!mouse.delta.ReadValueFromEvent(eventPtr, out Vector2 delta)) return;
@@ -47,15 +51,17 @@ public class InputCompareProbe : MonoBehaviour
void Update()
{
foreach (RawMouseInput.Event e in RawMouseInput.FrameEvents)
if (!unityMode)
{
rawHistory.Add(new Ev { t = e.t, dx = e.dx, dy = e.dy });
rawThisSecond++;
foreach (RawMouseInput.Event e in RawMouseInput.FrameEvents)
{
rawHistory.Add(new Ev { t = e.t, dx = e.dx, dy = e.dy });
rawThisSecond++;
}
}
double cut = Time.realtimeSinceStartupAsDouble - keepSeconds;
Trim(unityHistory, cut);
Trim(rawHistory, cut);
Trim(unityMode ? unityHistory : rawHistory, cut);
secondTimer += Time.unscaledDeltaTime;
if (secondTimer >= 1f)
@@ -64,7 +70,17 @@ public class InputCompareProbe : MonoBehaviour
unityThisSecond = rawThisSecond = 0; secondTimer = 0f;
}
if (Keyboard.current != null && Keyboard.current.f10Key.wasPressedThisFrame) Save();
if (Keyboard.current == null) return;
if (Keyboard.current.f11Key.wasPressedThisFrame) SwitchMode();
if (Keyboard.current.f10Key.wasPressedThisFrame) Save();
}
void SwitchMode()
{
unityMode = !unityMode;
if (unityMode) RawMouseInput.Stop(); // 還原成 Unity 原本的註冊
else RawMouseInput.Begin();
saveMessage = "";
}
static void Trim(List<Ev> list, double cut)
@@ -96,10 +112,11 @@ public class InputCompareProbe : MonoBehaviour
void OnGUI()
{
float y = Screen.height - 80;
GUI.Box(new Rect(10, y - 5, 640, 80), "");
GUI.Label(new Rect(20, y, 620, 22), $"Unity Input System : {unityPerSecond} 筆/秒 Raw Input : {rawPerSecond} 筆/秒 ({RawMouseInput.Status})");
GUI.Label(new Rect(20, y + 20, 620, 22), $"保留最近 {keepSeconds:F0} 秒 : Unity {unityHistory.Count} 筆、Raw {rawHistory.Count} 筆");
GUI.Label(new Rect(20, y + 40, 620, 22), string.IsNullOrEmpty(saveMessage) ? "移動滑鼠幾秒後按 F10 存到桌面 (unity_probe.csv / raw_probe.csv)" : saveMessage);
float y = Screen.height - 100;
GUI.Box(new Rect(10, y - 5, 700, 100), "");
GUI.Label(new Rect(20, y, 680, 22), $"目前模式 : {(unityMode ? "Unity Input System" : "Raw Input")} (F11 切換) Raw Input 狀態 : {RawMouseInput.Status}");
GUI.Label(new Rect(20, y + 20, 680, 22), $"每秒 : Unity {unityPerSecond} 筆、Raw {rawPerSecond} 筆");
GUI.Label(new Rect(20, y + 40, 680, 22), $"已錄 (最近 {keepSeconds:F0} 秒) : Unity {unityHistory.Count} 筆、Raw {rawHistory.Count} 筆");
GUI.Label(new Rect(20, y + 60, 680, 22), string.IsNullOrEmpty(saveMessage) ? "兩個模式都錄過之後按 F10 存到桌面" : saveMessage);
}
}
+2 -1
View File
@@ -177,7 +177,8 @@ public static class RawMouseInput
return RegisterRawInputDevices(rid, 1, (uint)Marshal.SizeOf<RAWINPUTDEVICE>());
}
static void Stop()
// 一般只在關遊戲時呼叫;InputCompareProbe 會暫時停掉,讓 Unity Input System 拿回滑鼠做對照
public static void Stop()
{
Application.quitting -= Stop;
if (!IsRunning) return;