x


Creating a Main Menu:

I'm having a problem with a script I'm trying to write. It's simple, but the answer's been evading me for several hours. I'm trying to create a main menu on a button click. Here's my script:

var customButton : GUIStyle;
var windowRect : Rect = Rect (20, 20, 120, 50);

function OnGUI () { 
    if (GUI.Button (Rect (1195, 6, 70, 20), "Menu", customButton)) {
        Menu();
        }
}

function WindowFunction (windowID : int) {
    // Draw any Controls inside the window here
}

function Menu () {
    windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
}

Is there anything absurdly simple I'm missing? Anything absurdly complex? I'm trying to learn how to use the new GUI, but I'm afraid it's beyond my grasp if someone doesn't give me a hand. Thanks for any and all answers -

Elliot Bonneville

more ▼

asked Mar 21 '10 at 06:40 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

What your code does is show the menu only when the the button has been clicked, but only for that frame. You need to set a variable when the button is clicked, then check every frame if the variable has been set, and draw the menu if it has. Something like this should work (untested):

var customButton : GUIStyle;
var windowRect : Rect = Rect (20, 20, 120, 50);
var showMenu : System.bool = false;

function OnGUI () { 
    if (GUI.Button (Rect (1195, 6, 70, 20), "Menu", customButton)) {
        showMenu = true;
    }
    if (showMenu) {
        Menu();
    }
}

function WindowFunction (windowID : int) {
    // Draw any Controls inside the window here
}

function Menu () {
    windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
}
more ▼

answered Mar 21 '10 at 06:54 PM

Stelimar gravatar image

Stelimar
3k 14 16 50

Thanks... That helped a lot.

Mar 21 '10 at 09:44 PM e.bonneville

You don't have to declare the boolean variable due to the automatic inference JS uses, by the way. Thanks again!

Mar 23 '10 at 04:36 PM e.bonneville

Thank you that helped me too

Dec 23 '10 at 06:11 AM Zachary
(comments are locked)
10|3000 characters needed characters left

I tried this and my script and this line,

var showMenu : System.bool = false;

wanted to be.

var showMenu : System.Boolean = false;

-kenm

more ▼

answered Mar 21 '10 at 10:54 PM

ken mcall gravatar image

ken mcall
31 2 2 4

Yeah, I forgot to post that I caught that. Problem solved... Thanks for the reply though.

Mar 21 '10 at 11:20 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Dec 08 '10 at 07:08 AM

Zachary gravatar image

Zachary
156 11 12 23

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3698
x386

asked: Mar 21 '10 at 06:40 PM

Seen: 4217 times

Last Updated: Mar 21 '10 at 09:44 PM