- Home /
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.
Have you considered upgrading to 4.6? The new UI tools make this implementation trivial.
not yet. im trying to finish this project first. but thanks for the tip.
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.
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.
im new to Unity and to classes too. only know the old structural codding. You give a nice example thanks for the tip.
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.