[C#] How to write a generic yield return WaitForCondition with an expression as argument ?

I wondered how one could write something like a yield return new WaitForCondition which allows you to wait until a condition is matched. I thought about making it with a helper function and a while - loop but I am not very satisfied with it, since it requires an unqiue WaitForCondition for each usage, as well as reference to a field and it does not allow expressions, like in if - statements, they are converted into a value, not preserving the expression as is.

In the end, I would like to be able to do something like this:

public class MyClass{
    bool myBool = false;
    public IEnumerator MyWait(){
        yield return new WaitForCondition(myBool == true);
        // doSomething
    }
}

I do know that in this example, I could instead write it similar to the accepted solution to this question: Waiting for Input via Coroutine

However, on purpose, I would like to require a bool expression as argument, no matter whether it is yield return new WaitForCondition( b == true );or yield return StartCoroutine(WaitForCondition(b == true));The examples might be misleading in terms of usability, imagine more complex bool expressions.

How would I achieve this ? I do know there are workarounds using Properties, but I would like to force this way. The main problem for me is preserving the expression as is instead of converting it to a value, as one is waiting for a condition to be matched, hence requiring to continuously checking for it.

This already exists: Unity - Scripting API: WaitUntil

You can use WaitUntil to do this:

yield return new WaitUntil( () => b == true );

If you’re like me and want a custom implementation of WaitUntil or WaitForSeconds, check out CustomYieldInstruction:

https://forum.unity.com/threads/writing-a-custom-class-yieldinstruction-to-wait-for-frames-or-seconds-depending-on-the-class-params.448925/