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 Firedan1176 · Feb 03, 2014 at 11:02 PM · guitextguilayoutabout-window

Can't add a GUI in C#??

OK I am a noob with GUIs in C# (mostly C# in general) and I'm trying to add an "about" page to my game. I have GUILayout.label for my button, but I can't add another one for the actual text for the about page. I want to get it to where I can show the text from a txt file (I know about text assets) and that's not the problem. The problem is that I can't add the new GUI that shows the information after I click the about button. Here's my code:

void OnGUI() { if (isLocked == false) { GUILayout.BeginArea (new Rect (0, 0, Screen.width, Screen.height)); GUILayout.BeginHorizontal (); GUILayout.FlexibleSpace (); GUILayout.BeginVertical (); GUILayout.FlexibleSpace (); GUI.skin = buttonSkinThing; if (GUILayout.Button ("About", "label")) { GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); [Whatever GUIText here to show text from file/about page] Time.timeScale = 1; Debug.Log ("Resume Game"); } if (GUILayout.Button ("Exit", "label")) { Debug.Log ("Exit Game"); } GUILayout.FlexibleSpace (); GUILayout.EndHorizontal (); GUILayout.FlexibleSpace (); GUILayout.EndVertical (); GUILayout.EndArea (); } else { Debug.Log ("CRAP"); } }

I know that some of it is junk like the flexible spaces and the endhorizontals, but all I need is for it to have a button that I can click that says "about". WHen I click that, I want it to go away and show another GUI with the text in a txt file. Anyone know why I'm messing this up?

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
0
Best Answer

Answer by Maui-M · Feb 04, 2014 at 10:36 PM

OnGui() is called constantly, so to change the GUIs displayed you will have to set up what is known as a state machine. This will allow you to display different GUIs based off of the current state of your application. Below is a simplified example of what you would probably want to implement.

 enum CurrentState = State.MainMenu;
 
 enum State {
     MainMenu = 0,
     About = 1,
     Gameplay = 2
 }
 
 void OnGUI() {
     if(CurrentState == State.MainMenu;) {
         // All your other main menu GUIs
         if (GUILayout.Button ("About", "label")) {
             Debug.Log ("About State");
             CurrentState = State.About;
         }
     }
     else if(CurrentState == State.About){
         // Show the about screen
         if (GUILayout.Button ("Exit", "label")) {
             Debug.Log ("Returning to menu");
             CurrentState = State.MainMenu;
         }
     }
     else if(CurrentState == State.Gameplay){
         // Show ingame GUIs
     }
 }


A cleaner version would be to use a case statement instead of a bunch of if else statements.

 enum CurrentState = State.MainMenu;
 
 enum State {
     MainMenu = 0,
     About = 1,
     Gameplay = 2
 }
 
 void OnGUI() {
     switch(CurrentState)
     {
         case State.MainMenu:
             MainMenuGUI();
             break;
         case State.About:
             AboutGUI();
             break;
         default:
             Debug.Log("NO CURRENT STATE");
             break;
     }
 }
 
 void MainMenuGUI() {
     // Main menu GUI code here
 }
 
 void AboutGUI() {
     // About menu GUI code here
 }
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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Similar function in UI for GUILayoutUtility.GetRect 0 Answers

Default text / Already written text on GUI Text Field. 1 Answer

How do i get size in pixels of a GUI.Label for chat construction purpose ? 2 Answers

Fix Blurry UI text? 10 Answers

How long is a string? 3 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