新增軌跡記錄器與匯出至unity內(滑鼠軌跡本身將改為raw input)
This commit is contained in:
1 parent
9e8b520bec
commit
14140797ba
30 files changed
+2299
-22
No files matched your search
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Controls;
|
||||
using UnityEngine.InputSystem.LowLevel;
|
||||
|
||||
// 逐筆抓原始輸入 : 每一筆滑鼠回報、每一次有效按鍵都帶自己的時間戳,不對齊到幀、不重新取樣
|
||||
public class InputEventCapture : MonoBehaviour
|
||||
{
|
||||
// t : 事件時間 (秒,跟 Time.realtimeSinceStartupAsDouble 同一條時間軸)
|
||||
public event Action<double, Vector2, bool> MouseEvent; // 時間、位移 (往右、往上為正)、左鍵
|
||||
public event Action<double, string, bool> KeyEvent; // 時間、按鍵名稱、按下 (true) / 放開 (false)
|
||||
|
||||
[Header("要記錄的按鍵 (遊戲裡有作用的才記)")]
|
||||
public Key[] trackedKeys = { Key.W, Key.A, Key.S, Key.D, Key.Space, Key.LeftShift, Key.R, Key.Digit1, Key.Digit2, Key.Digit3, Key.Digit4 };
|
||||
|
||||
[Header("除錯")]
|
||||
[Tooltip("在畫面左上角第二行顯示每秒滑鼠事件數與時間間隔,用來確認有沒有抓到每一筆")]
|
||||
public bool showDebug = true;
|
||||
|
||||
private bool[] keyDown;
|
||||
private int eventsThisSecond;
|
||||
private int eventsPerSecond;
|
||||
private double secondStart;
|
||||
private double lastEventTime;
|
||||
private double minGap = double.MaxValue;
|
||||
private double shownMinGap;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
// 預設 Input System 會把同一幀內的滑鼠事件合併成一筆,關掉才拿得到每一筆
|
||||
InputSystem.settings.disableRedundantEventsMerging = true;
|
||||
InputSystem.onEvent += OnInputEvent;
|
||||
keyDown = new bool[trackedKeys.Length];
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
InputSystem.onEvent -= OnInputEvent;
|
||||
}
|
||||
|
||||
void OnInputEvent(InputEventPtr eventPtr, InputDevice device)
|
||||
{
|
||||
if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>()) return;
|
||||
|
||||
if (device is Mouse mouse)
|
||||
{
|
||||
if (!mouse.delta.ReadValueFromEvent(eventPtr, out Vector2 delta)) return;
|
||||
mouse.leftButton.ReadValueFromEvent(eventPtr, out float left);
|
||||
|
||||
CountForDebug(eventPtr.time);
|
||||
MouseEvent?.Invoke(eventPtr.time, delta, left > 0.5f);
|
||||
}
|
||||
else if (device is Keyboard keyboard)
|
||||
{
|
||||
for (int i = 0; i < trackedKeys.Length; i++)
|
||||
{
|
||||
KeyControl key = keyboard[trackedKeys[i]];
|
||||
if (!key.ReadValueFromEvent(eventPtr, out float value)) continue;
|
||||
|
||||
bool down = value > 0.5f;
|
||||
if (down == keyDown[i]) continue;
|
||||
|
||||
keyDown[i] = down;
|
||||
KeyEvent?.Invoke(eventPtr.time, trackedKeys[i].ToString(), down);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 每秒事件數,以及這一秒內最小的事件間隔 (如果最小間隔接近 0 或剛好等於一幀,代表時間戳是批次蓋的)
|
||||
void CountForDebug(double t)
|
||||
{
|
||||
if (lastEventTime > 0) minGap = Math.Min(minGap, t - lastEventTime);
|
||||
lastEventTime = t;
|
||||
|
||||
eventsThisSecond++;
|
||||
if (t - secondStart >= 1.0)
|
||||
{
|
||||
eventsPerSecond = eventsThisSecond;
|
||||
shownMinGap = minGap == double.MaxValue ? 0 : minGap;
|
||||
eventsThisSecond = 0;
|
||||
minGap = double.MaxValue;
|
||||
secondStart = t;
|
||||
}
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if (!showDebug) return;
|
||||
GUI.Label(new Rect(10, 32, 600, 22),
|
||||
$"滑鼠事件 : {eventsPerSecond} 筆/秒 最小間隔 {shownMinGap * 1000.0:F3}ms (事件時間 {lastEventTime:F4}s / 幀時間 {Time.realtimeSinceStartupAsDouble:F4}s)");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user