Created
July 25, 2011 15:52
-
-
Save AngryAnt/1104439 to your computer and use it in GitHub Desktop.
An example of how combo keys could be implemented via coroutines (untested).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class KeyCombo | |
{ | |
public float maxPause = 1.0f; | |
public string[] keys; | |
public string message; | |
private int progress = 0; | |
public void Check (MonoBehaviour owner) | |
{ | |
if (progress == 0 && Input.GetKey (keys[0])) | |
// Start tracking keys if the first key is pressed | |
{ | |
owner.StartCoroutine (Run (owner)); | |
} | |
} | |
IEnumerator Run (MonoBehaviour owner) | |
{ | |
float start; | |
while (Application.isPlaying) | |
{ | |
if (progress >= keys.Length) | |
// If we typed all the keys, send the message and return | |
{ | |
owner.SendMessage (message); | |
progress = 0; | |
return; | |
} | |
else if (Input.GetKey (keys[progress])) | |
// If the next key was pressed, move forward and reset the timer | |
{ | |
progress++; | |
start = Time.time; | |
} | |
else if (Time.time - start > maxPause || | |
// Reset and return if we time out | |
( | |
Input.anyKey && | |
!( | |
Input.GetMouseButton (0) || | |
Input.GetMouseButton (1) || | |
Input.GetMouseButton (2) | |
) | |
) | |
// Or if an incorrect key was typed | |
// NOTE: Might want to expand what is allowed here | |
) | |
{ | |
progress = 0; | |
return; | |
} | |
yield return null; | |
} | |
} | |
} | |
public class ComboKeys : MonoBehaviour | |
{ | |
public KeyCombo[] combos; | |
void Update () | |
{ | |
foreach (KeyCombo combo in combos) | |
{ | |
combo.Check (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment