I’m new to coroutines, but I think what I’m wanting to do here is a good use for them, I just can’t figure out how to do it. What I want to do is create a sort of wizard, where you step through a multi-step process each time the user clicks a button. I have created a rough sketch that I think conveys what I mean:
How do I set up a coroutine to sit and loop on a step until the mouse is clicked, then move on to the next step. While in the ‘wizard’ portion of the code, “Do Something” should not execute.
I know how I can do this using boolean flags or an integer to represent what ‘step’ I’m on, but I’d like to avoid that if possible.
function ButtonLoop() {
var currentStage: int = 0;
var done : int = 4;
while ( currentStage < done ) {
currentStage = Display( currentStage );
yield;
}
}
function Display( currentStage : int ) : int {
var returnValue : int = currentStage
switch( currentStage ) {
case 1:
// display/check button status, set returnValue to 2 if "next" was pressed, 0 if "back" was pressed
break;
/*...*/
}
return returnValue;
}
If you’re dead-set against state variables, you can jam it all into the coroutine:
function ButtonLoop() {
while( !done ) {
// do stage one, set done when done
yield;
}
while( !done ) {
// do stage two, set done when done
yield;
}
while( !done ) {
// do stage three, set done when done
yield;
}
}
There will be complications with that, but they’ll be fixable. (going back will be difficult, for example)
Edit: If you’re using OnGUI, you already essentially -have- your coroutine, since it has to run all the time anyway. It’s easiest to use a state machine with OnGUI.