Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created January 6, 2015 08:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mstevenson/786d37a63192bda0d65f to your computer and use it in GitHub Desktop.
Save mstevenson/786d37a63192bda0d65f to your computer and use it in GitHub Desktop.
Bind key presses to methods in Unity at runtime
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public delegate void InputKeyDelegate (KeyCode key);
public delegate void InputAxisDelegate (string name,float value);
public class InputEvents : MonoBehaviour
{
private static Dictionary<KeyCode, List<InputKeyDelegate>> keyDownsDict = new Dictionary<KeyCode, List<InputKeyDelegate>> ();
private static Dictionary<KeyCode, List<InputKeyDelegate>> keysDict = new Dictionary<KeyCode, List<InputKeyDelegate>> ();
private static Dictionary<KeyCode, List<InputKeyDelegate>> keyUpsDict = new Dictionary<KeyCode, List<InputKeyDelegate>> ();
private static Dictionary<string, List<InputAxisDelegate>> axesDict = new Dictionary<string, List<InputAxisDelegate>> ();
#region Generic Binders
private static void BindKeys (Dictionary<KeyCode, List<InputKeyDelegate>> keyDelegateDict, InputKeyDelegate method, params KeyCode[] keys)
{
foreach (KeyCode key in keys) {
List<InputKeyDelegate> delegateList;
keyDelegateDict.TryGetValue (key, out delegateList);
if (delegateList == null) {
delegateList = new List<InputKeyDelegate> ();
keyDelegateDict.Add (key, delegateList);
}
delegateList.Add (method);
}
}
private static void UnbindKeys (Dictionary<KeyCode, List<InputKeyDelegate>> keyDelegateDict, InputKeyDelegate method, params KeyCode[] keys)
{
foreach (KeyCode key in keys) {
List<InputKeyDelegate> delegateList;
keyDelegateDict.TryGetValue (key, out delegateList);
if (delegateList != null) {
for (int i = 0; i < delegateList.Count; i++) {
if (delegateList [i] == method) {
delegateList.RemoveAt (i);
}
}
}
}
}
#endregion
#region Binding Methods
public static void BindKeyDown (InputKeyDelegate method, params KeyCode[] keys)
{
BindKeys (keyDownsDict, method, keys);
}
public static void UnbindKeyDown (InputKeyDelegate method, params KeyCode[] keys)
{
UnbindKeys (keyDownsDict, method, keys);
}
public static void BindKeyUp (InputKeyDelegate method, params KeyCode[] keys)
{
BindKeys (keyUpsDict, method, keys);
}
public static void UnBindKeyUp (InputKeyDelegate method, params KeyCode[] keys)
{
UnbindKeys (keyUpsDict, method, keys);
}
public static void BindKey (InputKeyDelegate method, params KeyCode[] keys)
{
BindKeys (keysDict, method, keys);
}
public static void UnBindKey (InputKeyDelegate method, params KeyCode[] keys)
{
UnbindKeys (keysDict, method, keys);
}
public static void BindAxis (InputAxisDelegate method, params string[] axes)
{
foreach (string axis in axes) {
List<InputAxisDelegate> delegateList;
axesDict.TryGetValue (axis, out delegateList);
if (delegateList == null) {
delegateList = new List<InputAxisDelegate> ();
axesDict.Add (axis, delegateList);
}
delegateList.Add (method);
}
}
public static void UnbindAxis (InputAxisDelegate method, params string[] axes)
{
foreach (string axis in axes) {
List<InputAxisDelegate> delegateList;
axesDict.TryGetValue (axis, out delegateList);
if (delegateList != null) {
for (int i = 0; i < delegateList.Count; i++) {
if (delegateList [i] == method) {
delegateList.RemoveAt (i);
}
}
}
}
}
#endregion
#region Unity Events
public void Update ()
{
foreach (var kvp in keyDownsDict) {
if (Input.GetKeyDown (kvp.Key)) {
foreach (InputKeyDelegate method in kvp.Value) {
method (kvp.Key);
}
}
}
foreach (var kvp in keysDict) {
if (Input.GetKey (kvp.Key)) {
foreach (InputKeyDelegate method in kvp.Value) {
method (kvp.Key);
}
}
}
foreach (var kvp in keyUpsDict) {
if (Input.GetKeyUp (kvp.Key)) {
foreach (InputKeyDelegate method in kvp.Value) {
method (kvp.Key);
}
}
}
foreach (var kvp in axesDict) {
float val = Input.GetAxis (kvp.Key);
foreach (InputAxisDelegate method in kvp.Value) {
method (kvp.Key, val);
}
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment