Skip to content

Instantly share code, notes, and snippets.

@areyoutoo
Created April 18, 2014 02:25
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save areyoutoo/11021717 to your computer and use it in GitHub Desktop.
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