123 lines
4.8 KiB
C#
123 lines
4.8 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 對照工具 (開發用,不要放進正式場景)
|
|
// 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("每個模式最多保留最近幾秒")]
|
|
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 = "";
|
|
|
|
void OnEnable()
|
|
{
|
|
// 預設 Input System 會把同一幀內的滑鼠事件合併成一筆,關掉才拿得到每一筆
|
|
InputSystem.settings.disableRedundantEventsMerging = true;
|
|
InputSystem.onEvent += OnInputEvent;
|
|
}
|
|
|
|
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;
|
|
if (delta == Vector2.zero) return; // 只算有位移的回報,跟 Raw Input 一樣
|
|
|
|
unityHistory.Add(new Ev { t = eventPtr.time, dx = delta.x, dy = delta.y });
|
|
unityThisSecond++;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!unityMode)
|
|
{
|
|
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(unityMode ? unityHistory : rawHistory, cut);
|
|
|
|
secondTimer += Time.unscaledDeltaTime;
|
|
if (secondTimer >= 1f)
|
|
{
|
|
unityPerSecond = unityThisSecond; rawPerSecond = rawThisSecond;
|
|
unityThisSecond = rawThisSecond = 0; secondTimer = 0f;
|
|
}
|
|
|
|
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)
|
|
{
|
|
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 - 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);
|
|
}
|
|
}
|