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