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 HHameline · Apr 28, 2011 at 05:43 PM · uidisplayissuesupdating

OnGUI and Displaying GUI Features

Ok right now I have a vague beginnings of my UI. The buttons get added to the scroll area and then track each button clicked, in order to open a GUI.Window item which I haven't implemented yet.

void OnGUI() {

 AutoItemLayout();
 for (int i = 0; i < buttons.Count; i++)
 {
     if (buttons[i])
     {                
        //do something 
     }
 }

}

void AutoItemLayout() { GUI.enabled = windowNotOpen; GUILayout.BeginArea(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT)); GUI.Box(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT), "");

 category = GUI.Toolbar(new Rect(5, 5, SHOP_WIDTH - 10, 85), category, categoryName);              

 GUI.Box(new Rect(5, 95, (SHOP_WIDTH - 10), (SHOP_HEIGHT - 100)), "");
 scrollViewVector = GUI.BeginScrollView(new Rect(10, 100, (SHOP_WIDTH - 20), (SHOP_HEIGHT - 110)), scrollViewVector, new Rect(0, 0, 0, scrollHeight));

 if (category != currentCategory)
 {
     buttons.Clear();
     switch (category)
     {
         case 0:
             displayItems = clothes;
             currentCategory = 0;                    
             break;
         case 1:
             displayItems = playground;
             currentCategory = 1;                    
             break;
         case 2:
             displayItems = music;
             currentCategory = 2;                    
             break;
         case 3:
             displayItems = zopet;
             currentCategory = 3;                   
             break;
         default:
             Debug.Log("SHOP TOOLBAR BROKE");
             break;
     }
     setUpShopLayout();  
     for (int i = 0; i < buttonPositions.Count; i++)
     {
         buttons.Add(GUI.Button(buttonPositions[i], displayItems[i].ItemName));
     }         
 }       

 GUI.EndScrollView();
 GUILayout.EndArea();
 GUI.enabled = true;

}

public void setUpShopLayout() { buttonPositions.Clear(); count = 0; noExtraItems = true; total = displayItems.Count;

 lessThan = (int)(SHOP_WIDTH - 35) / (buttonWidth); ;
 buttonBuffer = (int)(SHOP_WIDTH - 35 - (buttonWidth * lessThan)) / lessThan;
 scrollHeight = ((total / lessThan) * (buttonHeight + buttonBuffer));
 numrows = total / lessThan;

 if ((total % lessThan) > 0)
 {
     extraItems = total % lessThan;
     numrows++;
     scrollHeight = scrollHeight + (buttonHeight + buttonBuffer);
     noExtraItems = false;
 }

 for (int i = 0; i < (numrows); i++)
 {
     if ((i + 1) < numrows || noExtraItems)
     {
         for (int j = 0; j < lessThan; j++)
         {
             name = displayItems[count].ItemName;
             buttonPositions.Add(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight));
             count++;
         }
     }
     else
     {
         for (int j = 0; j < extraItems; j++)
         {
             name = displayItems[count].ItemName;
             buttonPositions.Add(new Rect((j * (buttonWidth + buttonBuffer)), (i * (buttonHeight + buttonBuffer)), buttonWidth, buttonHeight));
             count++;
         }
     }
 }

}

That's the main chunk of my code. However the loop, that adds the GUI.Button to the buttons array, is infinity adding buttons outside of the if(category != currentCategory) statement, and inside the if statement it adds the correct number of buttons, however they do not appear on the UI. Any ideas?

Thanks, Hans Unity Newb

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Apr 28, 2011 at 05:55 PM

Unity uses an immediate GUI system. That means ObGUI is called every frame and you have to draw your GUI every frame. You can't create a GUI and just display it. At the moment you stop calling GUI.Button() the button will disappear.

Also take a look at GUILayout that provides the same elements as GUI but the size and position is determined automatically. You can use horizontal or vertical groups to design your layout. Those groups can be nested. With GUILayoutOptions like Width you still can influence the layouting

Comment
Add comment · Show 4 · 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 HHameline · Apr 28, 2011 at 05:59 PM 0
Share

Thanks much more elegant than my current method. Greatly appreciated.

avatar image Bunny83 · Apr 28, 2011 at 06:00 PM 0
Share

I just realised that you already mixed GUI and GUILayout. But that makes no sense at all. If you want a relative area for the normal GUI stuff use GUI.BeginGroup.

avatar image HHameline · Apr 28, 2011 at 06:04 PM 0
Share

Jeepers, Unity just has so much stuff to get used to, hard to keep track of it all. Thanks again for the help.

avatar image Bunny83 · Apr 28, 2011 at 06:08 PM 0
Share

No problem ;) Unity gets quite handy once you figured out how it works...

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

No one has followed this question yet.

Related Questions

Display Ammo Count Above Player 1 Answer

Best way to show variable values on UI 1 Answer

Ignoring/not displaying text/numbers in string if surrounded with (), [] or <>? 1 Answer

Odd veriable and UI issue 1 Answer

[Mobile] Performance issues on 4k displays 0 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