Is it possible to make a custom WaitFor?

I have an coroutine and want it to wait until any key is pressed.
Now I could do it without a Coroutine using a while loop and the update function to look for Input.anyKeyDown…
But is it possible to write an own WaitFor function which does this while loop and checking the update function?
It seems to be a little bit complicated for such an easy thing but it could save much time which I had to spent on doing the same while loop thing for every script…

Sure that’s pretty easy:

IEnumerator WaitForKeyDown(KeyCode aKey)
{
    while (!Input.GetKeyDown(aKey))
        yield return null;
}

This function can be used like this:

IEnumerator Somecoroutine()
{
    // do something
    yield return StartCoroutine(WaitForKeyDown(KeyCode.A));
    // do something else
}

If you want to get rid of the StartCoroutine call, you can wrap your utility functions in a seperate MonoBehaviour-singleton which will run the coroutines:

public class WaitFor : MonoBehaviour
{
    private static WaitFor m_Instance = null;
    public static WaitFor Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = (WaitFor)FindObjectOfType(typeof(WaitFor));
                if (m_Instance == null)
                    m_Instance = (new GameObject("WaitForHelper")).AddComponent<WaitFor>();
            }
            return m_Instance;
        }
    }
    private static IEnumerator _WaitForKeyDown(KeyCode aKey)
    {
        while (!Input.GetKeyDown(aKey))
            yield return null;
    }
    public static Coroutine KeyDown(KeyCode aKey)
    {
        return Instance.StartCoroutine(_WaitForKeyDown(aKey));
    }
    // [...]
}

With this class you can simply do this:

IEnumerator Somecoroutine()
{
    // do something
    yield return WaitFor.KeyDown(KeyCode.A);
    // do something else
}

Sure you can do that:

   IEnumerator WaitFor(string buttonName)
   {
        while(!Input.GetButtonDown(buttonName)) yield return null;
   }

Then to use it:

    IEnumerator SomeCoroutine()
    {
        //Do something
        yield return StartCoroutine(WaitFor("fire1"));
        //Do something else
    }