title: Unity Coroutine Wait Until true for x seconds
body: <p>I wanted to implement something that works similar to <code>yield return new WaitUntil(() =&gt; Check());</code>, but with one extra addition. After the <code>Check()</code> condition is met it should wait x seconds checking every frame if the condition is still true.</p>
  <p>This is my implementation:</p>
  <pre class="lang-cs prettyprint-override"><code>private IEnumerator CheckCor(float waitTime)
  {
      bool checkFlag = true;
      bool checkFlag2;
      float whileTime;
      while (checkFlag)
      {
          yield return new WaitUntil(() =&gt; Check());
          checkFlag2 = true;
          whileTime = waitTime;
          while (whileTime &gt; 0)
          {
              if (!Check())
              {
                  checkFlag2 = false;
              }
              whileTime -= Time.deltaTime;
              yield return null;
          }
          if (checkFlag2)
          {
              checkFlag = false;
          }
      }
  }
  </code></pre>
  <p>where <code>Check()</code> is</p>
  <pre class="lang-cs prettyprint-override"><code>private bool Check();
  </code></pre>
  <p>My implementation is working perfectly fine, but it seems a bit long.</p>
  <p>Is there any shorter way to achieve the same behavior?</p>
  <p>(Also making it universal would be a plus e.g. <code>yield return WaitUntilForSeconds(Check(), 3f);</code>, where Check() is the condition and 3f is time to check every frame for the condition. I'm guessing it could be done using <code>CustomYieldInstruction</code>, but I'm not sure how it works.)</p>