106 lines
4.1 KiB
C#
106 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
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
|
|
public class InputCompareProbe : MonoBehaviour
|
|
{
|
|
[Tooltip("按 F10 存檔時保留最近幾秒")]
|
|
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 int unityThisSecond, rawThisSecond, unityPerSecond, rawPerSecond;
|
|
private float secondTimer;
|
|
private string saveMessage = "";
|
|
|
|
void OnEnable()
|
|
{
|
|
// 預設 Input System 會把同一幀內的滑鼠事件合併成一筆,關掉才拿得到每一筆
|
|
InputSystem.settings.disableRedundantEventsMerging = true;
|
|
InputSystem.onEvent += OnInputEvent;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
InputSystem.onEvent -= OnInputEvent;
|
|
}
|
|
|
|
void OnInputEvent(InputEventPtr eventPtr, InputDevice device)
|
|
{
|
|
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>()) return;
|
|
if (!(device is Mouse mouse)) return;
|
|
if (!mouse.delta.ReadValueFromEvent(eventPtr, out Vector2 delta)) return;
|
|
if (delta == Vector2.zero) return; // 只算有位移的回報,跟 Raw Input 一樣
|
|
|
|
unityHistory.Add(new Ev { t = eventPtr.time, dx = delta.x, dy = delta.y });
|
|
unityThisSecond++;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
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);
|
|
|
|
secondTimer += Time.unscaledDeltaTime;
|
|
if (secondTimer >= 1f)
|
|
{
|
|
unityPerSecond = unityThisSecond; rawPerSecond = rawThisSecond;
|
|
unityThisSecond = rawThisSecond = 0; secondTimer = 0f;
|
|
}
|
|
|
|
if (Keyboard.current != null && Keyboard.current.f10Key.wasPressedThisFrame) Save();
|
|
}
|
|
|
|
static void Trim(List<Ev> list, double cut)
|
|
{
|
|
int remove = 0;
|
|
while (remove < list.Count && list[remove].t < cut) remove++;
|
|
if (remove > 0) list.RemoveRange(0, remove);
|
|
}
|
|
|
|
void Save()
|
|
{
|
|
string desktop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
|
|
Write(Path.Combine(desktop, "unity_probe.csv"), unityHistory);
|
|
Write(Path.Combine(desktop, "raw_probe.csv"), rawHistory);
|
|
saveMessage = $"已存 Unity {unityHistory.Count} 筆、Raw {rawHistory.Count} 筆到桌面";
|
|
}
|
|
|
|
static void Write(string path, List<Ev> list)
|
|
{
|
|
StringBuilder sb = new StringBuilder("t,dx,dy\n");
|
|
foreach (Ev e in list)
|
|
{
|
|
sb.Append(e.t.ToString("F6", CultureInfo.InvariantCulture)).Append(',')
|
|
.Append(e.dx.ToString(CultureInfo.InvariantCulture)).Append(',')
|
|
.Append(e.dy.ToString(CultureInfo.InvariantCulture)).Append('\n');
|
|
}
|
|
File.WriteAllText(path, sb.ToString());
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|