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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

Similar function in UI for GUILayoutUtility.GetRect 0 Answers

Gui Button Solid 2 Answers

How to save and load text in a GUI TextArea 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges