Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using UnityEngine;
using System.Collections.Generic;
public class HighFreqInput : MonoBehaviour {
struct InputEvent {
public readonly bool keyUp;
public readonly KeyCode keyCode;
public readonly int frame;
public readonly float time;
public readonly string msg;
public InputEvent(bool ku, KeyCode kc, int f, float t) {
keyUp = ku;
keyCode = kc;
frame = f;
time = t;
msg = string.Format("{0} {1} | frame {2} | time {3}", kc.ToString(), ku ? "up" : "down", f, t);
}
}
List<InputEvent> events = new List<InputEvent>();
void OnGUI() {
Event e = Event.current;
if (e.isKey) {
EventType type = e.type;
switch (e.type) {
case EventType.KeyDown:
events.Add(new InputEvent(false, e.keyCode, Time.frameCount, Time.realtimeSinceStartup));
break;
case EventType.KeyUp:
events.Add(new InputEvent(true, e.keyCode, Time.frameCount, Time.realtimeSinceStartup));
break;
}
}
if (e.type == EventType.Repaint) {
float f = 0f;
const float STEP = 20f;
foreach (InputEvent ev in events) {
GUI.Label(new Rect(0f, f, 500f, STEP), ev.msg);
f += STEP * 0.9f;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment