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 Lyonmen · Nov 28, 2014 at 07:32 PM · ongui

call a function from inside OnGUI

Hi.

I'm codding an App made of entire GUI. For that i use something like this:

 void OnGui () {
    if (menu == "main") {
       // show content of menu
    }
    else if (menu == "options") {
       // show content of options
    }
 }

My problem is, i have about 50 menu and my code was getting too long. so every time i want to made something in 2 or 3 menus i need to scroll a lot on my code. I was thinking on change my code to something like this:

 void OnGui () {
    if (menu == "main") {
       MainScript.ShowContent();
    }
    else if (menu == "options") {
       OptionScript.ShowContent();
    }
 }

In that case every menu was a separeted script with only the content of that menu, so if i need to change something in 2 or 3 scripts i just select another window on editor.

But i have no idea how to do this and if this is even possible. Can someone please help me with this? Thanks in advance.

Note: Sorry if i wrote something wrong, English is not my usual Language.

Comment
Add comment · Show 2
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 Kiwasi · Nov 28, 2014 at 08:21 PM 0
Share

Have you considered upgrading to 4.6? The new UI tools make this implementation trivial.

avatar image Lyonmen · Dec 01, 2014 at 10:57 AM 0
Share

not yet. im trying to finish this project first. but thanks for the tip.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Unitraxx · Nov 28, 2014 at 07:39 PM

You can perfectly do this and there is really nothing special about it, you just need to call the other function.

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
0
Wiki

Answer by fafase · Nov 28, 2014 at 08:19 PM

Well if you get a real long menu you can store all the methods in a different class and use some arrays:

 string[]texts = new string[]{"main", "options"};
 Rect[] rects;
 Action [] actions;
 
 void Start()
 {
    rects = new Rect[texts.Length];
    for(int i = 0; i < rects.Length; i++)
    {
        rects[i] = new Rect(20 * i, 0, 20,20);
    }
    Script s = GetComponent<Script>();
    actions[0] = s.MethodA;
    actions[1] = s.MethodB;
    // So on
 }
 
 void OnGUI()
 {
    for(int i = 0; i < texts.Length; i++)
    {
        if(GUI.Button(rects[i], texts[i])){
           actions[i];
        }
    }
 }

But best would be to use the new system, where only the methods are necessary, the rest is manual placement.

Comment
Add comment · Show 1 · 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 Lyonmen · Dec 01, 2014 at 11:03 AM 0
Share

im new to Unity and to classes too. only know the old structural codding. You give a nice example thanks for the tip.

avatar image
0

Answer by Lyonmen · Dec 01, 2014 at 01:58 PM

after some hours of tests i came to this:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AppController : MonoBehaviour {
     public Main_Screen Main_Screen;                // Script with the main screen
     public Option_Screen Option_Screen;            // Script with the options screen
     public Language_Screen Language_Screen;        // Script with the language options screen
     public string menu = "main";
     
     void OnGUI() {   
         if (menu == "main") {
             Main_Screen.ShowContent();
         }
         else if (menu == "options") {
             Option_Screen.ShowContent();
         }
         else if (menu == "language") {
             Language_Screen.ShowContent();
         }
     }
 }

Now on other files i notice i can go on two diferent ways to access another scripts. Creating a variable for that specific script like this:

 public Option_Screen Option_Screen;            // Script with the options screen
 public Language_Screen Language_Screen;        // Script with the language options screen

or just creating one for the main script AppController and call the variables and functions like this:

 public AppController AppController ;            // main script
 if (AppController.Option_Screen.somevariable == "somevalue") {
     // do something
 }

Now the question is, witch way is better?

Sorry again if i wrote something wrong and if those question are to damm borrung and noob.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drawing a GUI box by specifying the two corners 1 Answer

Placing a chat bubble above a character's head in a 3d environment 2 Answers

Saving a button in PlayerPrefs 2 Answers

Editor GUI Drag Selection Box 1 Answer

How does this script identify other players? C# 1 Answer


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