You can also put your GUI into booleans or cases:
var isStarting = true;
var isOptions = false;
function OnGUI () {
if (isStarting) {
if (GUI.Button (Rect (10, 10, 100, 20), "Options")) {
isStarting = false;
isOptions = true;
}
if (GUI.Button (Rect (10, 40, 100, 30), "Play")) {
Application.LoadLevel (1);
}
}
if (isOptions) {
if (GUI.Button (Rect (10, 10, 100, 20), "Volume")) {
Debug.Log ("Volume Button");
}
if (GUI.Button (Rect (10, 40, 100, 20), "Sensitivity")) {
Debug.Log ("Sensitivity Button");
}
if (GUI.Button (Rect (10, 70, 100, 20), "Done")) {
isStarting = true;
isOptions = false;
}
}
}
Or you can use an enum and a switch, which is more efficient:
enum UIState {Starting, Options}
var uIState : UIState;
function Start () {
uIState = UIState.Starting;
}
function OnGUI () {
switch (uIState) {
case UIState.Starting:
if (GUI.Button (Rect (10, 10, 100, 20), "Options")) {
uIState = UIState.Options;
}
if (GUI.Button (Rect (10, 40, 100, 20), "Play")) {
Application.LoadLevel (1);
}
break;
case UIState.Options:
if (GUI.Button (Rect (10, 10, 100, 20), "Volume")) {
Debug.Log ("Volume Button");
}
if (GUI.Button (Rect (10, 40, 100, 20), "Sensitivity")) {
Debug.Log ("Sensitivity Button");
}
if (GUI.Button (Rect (10, 70, 100, 20), "Done")) {
uIState = UIState.Starting;
}
break;
}
}
This way you can keep it all in one script.
Further: GUI code doesn’t need to reside inside OnGUI as long as it’s called from OnGUI, so you can organize your code in a more disassociated fashion for easy maintenance:
enum UIState {Starting, Options}
var uIState : UIState;
function Start () {
uIState = UIState.Starting;
}
function OnGUI () {
switch (uIState) {
case UIState.Starting:
GUIStarting ();
break;
case UIState.Options:
GUIOptions ();
break;
}
}
function GUIStarting () {
if (GUI.Button (Rect (10, 10, 100, 20), "Options")) {
uIState = UIState.Options;
}
if (GUI.Button (Rect (10, 40, 100, 20), "Play")) {
Application.LoadLevel (1);
}
}
function GUIOptions () {
if (GUI.Button (Rect (10, 10, 100, 20), "Volume")) {
Debug.Log ("Volume Button");
}
if (GUI.Button (Rect (10, 40, 100, 20), "Sensitivity")) {
Debug.Log ("Sensitivity Button");
}
if (GUI.Button (Rect (10, 70, 100, 20), "Done")) {
uIState = UIState.Starting;
}
}