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.
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 _= new Rect(20 * i, 0, 20,20);_
} Script s = GetComponent(); actions[0] = s.MethodA; actions[1] = s.MethodB; // So on }
void OnGUI() { for(int i = 0; i < texts.Length; i++) { if(GUI.Button(rects_, texts*)){_ _actions; } } }* But best would be to use the new system, where only the methods are necessary, the rest is manual placement._
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.