- Home /
Making a real coroutine
I need to make structural GUI like something in the following scheme:
Button 1 Button 2
|| /\ || /\
\/ || | \/ ||
Button 1.1 Button 1.2 Back | Button 2.1 Button 2.2 Back
Each level is displayed alone - they are not stacked in height, they change lines by clicking buttons (e.g. Only Button 1 and 2 are displayed - clicking Button 1 will show only Buttons 1.1 and 1.2 and Back).
I thought of making a coroutine that will be something like:
IEnumerator GetGUIInput()
{
while(true)
{
if(GUI.Button(new Rect(0, 0, 100, 100), "Button 1"))
{
while(!GUI.Button(new Rect(200, 0, 100, 100), "Back"))
{
if(GUI.Button(new Rect(0, 0, 100, 100), "Button 1.1"))
{
// Do something
break;
}
else if(GUI.Button(new Rect(100, 0, 100, 100), "Button 1.2"))
{
// Do something
break;
}
yield return 0;
}
}
else if(GUI.Button(new Rect(100, 0, 100, 100), "Button 2"))
{
while(!GUI.Button(new Rect(200, 0, 100, 100), "Back"))
{
if(GUI.Button(new Rect(0, 0, 100, 100), "Button 2.1"))
{
// Do something
break;
}
else if(GUI.Button(new Rect(100, 0, 100, 100), "Button 2.2"))
{
// Do something
break;
}
yield return 0;
}
}
yield return 0;
}
}
But it won't work.... because GUI.Button works only within OnGUI.
So I mixed up this:
IEnumerator GUIInputState;
void Start() { GUIInputState = GetGUIInput(); } void OnGUI() { GUIInputState.MoveNext(); }
Which works fine.... but it seems like rewriting StartCoroutine, and it will turn into a complete mess when I will need things like
yield return new WaitForSeconds(1);
Is there a way of making a coroutine be called only within OnGUI or I have to rewrite StartCoroutine?
Are you sure you need a coroutine? I would think standard OnGUI scripting would accomplish this.
It cannot - after pressing Button 1 I will need to start using states to know what state it is in and it will get messy.
Answer by Molix · Apr 06, 2011 at 08:26 PM
I don't think you need a coroutine, but you do need to store some state. The easiest would be to encapsulate each button in a tiny class. It may seem like overkill, but notice how simple the OnGUI() is, and you can easily extend the functionality without writing any more GUI.
public class ButtonCommand { public string buttonText; public delegate void OnClick(); public OnClick onClick; }
public class Menu { public ButtonCommand[] buttons; }
public Menu[] menus; // e.g. a 1+2 menu, a 1.1+1.2 menu, a 2.1+2.2 menu public Menu activeMenu;
void OnGUI() { GUILayout.BeginHorizontal(); foreach( ButtonCommand button in activeMenu.buttons ) { if( GUILayout.Button( button.buttonText ) ) button.onClick(); } GUILayout.EndHorizontal(); }
This is just for illustration of course (e.g you'll need to actually instantiate the buttons and menus you want).
Answer by Bampf · Apr 06, 2011 at 08:47 PM
You will have to track the states somehow.
If you just have a handful of menus you can simply assign each one a number, or define an enum, and have an instance variable track which menu is active. If you define one menu method per menu, OnGUI would just call the method belonging to whichever menu is active.
Or you can get fancier. The Unify wiki has an example that uses C# delegates. To switch to a different menu, it assigns the delegate to point to a different menu method.
And as I write this I see Molix has just posted an answer that uses yet another way. It defines a menu object, each a list of buttons to draw. The script keeps an array of these menus, and only one is active at any given time.
It's true that coroutines, if they'd been usable here, might mean you wouldn't have to track the menu state explicitly. But as you can see there are many alternatives and they aren't necessarily messy.
Answer by Bunny83 · May 23, 2011 at 01:49 AM
I'd like to add that you can use coroutines but, like you said, you have to implement your own scheduler. In the unify-wiki is a great simple CoroutineScheduler which does exactly what you want ;). Since the build-in classes of unity are sealed and in addition you can't access their private fields you can't use WaitForSeconds and stuff like that, but Fernando Zapata implemented a "wait for seconds" function and it's even simpler ;). You can't implement things like WaitForEndOfFrame because you will run your scheduler only in OnGui().
Your answer
Follow this Question
Related Questions
Animate GUI elements by code? 1 Answer
Stopping a Coroutine and drawing OnGUI 1 Answer
Terminal-like GUI, wait for input 1 Answer
How to make a GUI button 1 Answer
Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers