Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dani · Apr 06, 2011 at 07:02 PM · guicoroutine

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Apr 06, 2011 at 07:51 PM 0
Share

Are you sure you need a coroutine? I would think standard OnGUI scripting would accomplish this.

avatar image Dani · Apr 06, 2011 at 08:02 PM 0
Share

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.

avatar image Bampf · Apr 06, 2011 at 08:23 PM 1
Share

$$anonymous$$essy is not the same as "cannot". :-)

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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().

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges