- Home /
Create a sliding Menu
Hi all, I am just learning Unity 3 and I am finding the learning curve quite steep for me.
At this point of time, I've managed to do up a decent GUI interface with GUIskin and GUI Group. If let's say I would like to make the whole GUI group slide out of the menu upon keypress(spacebar), which command should I be using? Am I doing it right in the first place because I can't seem to get the whole GUI Group to move on keypress.
Below is my code:
@script ExecuteInEditMode()
//This command is to run your GUI scripts without having to play the scene
//variable for GUISkin var customSkin : GUISkin;
var icon : Texture;
var plate : Texture;
//Slider default values when you first run Unity. var slider = 50;
//var windowRect : Rect = Rect (20, 20, 120, 50);
function OnGUI () {
//applies skin to all. any GUI elements above this script will not get "skinned". GUI.skin = customSkin;
//Everything in a group will move accordingly is you adjust the x and y values. GUI.BeginGroup (Rect (Screen.width/2-480,Screen.height/2-240, 960,480 ));
// We'll make a box so you can see where the group is on-screen. //GUI.Box (Rect (0,0,960,480), "Interface Group 1");
GUI.Label (Rect ( 0, 0, 960, 480), plate);
//To set the button to load levels, add your scenes from the Build Settings. if (GUI.Button (Rect (60,25,80,30), "Level 1")) { Application.LoadLevel (1); }
GUI.Button (Rect (280,25,80,30), "Button 2");
GUI.Button (Rect (500,25,80,30), icon);
GUI.Label (Rect (760, 135, 128,128),icon);
// End the group we started above. GUI.EndGroup ();
}
//////What command should I use here for the keypress function?/////////////
function Update () { if (Input.GetKeyDown (KeyCode.Space)){
print ("space key was pressed");
//(command to use here?);
}
}
Answer by rustystar · Oct 27, 2010 at 08:08 AM
I've already managed to add the function in by using variables assigned to the x coordinates and rewriting the whole structure.
Many thanks to HiggyB!
Below are the working scripts: @script ExecuteInEditMode()
var MyBoxLeft = -200.0;
var icon : Texture; var icon2 : Texture; var blankSkin : GUISkin;
function OnGUI () { GUI.skin = blankSkin; var tBoxRect = new Rect(MyBoxLeft, 20.0, 220, 720); var tButtonRect = new Rect(MyBoxLeft, 160.0, 120, 30); var tBoxArt = new Rect(MyBoxLeft, 200.0, 120, 120);
GUI.Box(tBoxRect, icon); GUI.Button(tButtonRect, "Click"); GUI.Box(tBoxArt, icon2); }
function Update () {
if (Input.GetKeyDown("space")) { AnimateBox(); }
}
function AnimateBox () {
if (MyBoxLeft == -200.0) {
while (MyBoxLeft < 20.0) {
MyBoxLeft+=5.0;
yield;
}
} else if (MyBoxLeft == 20.0) {
while (MyBoxLeft > -200.0) {
MyBoxLeft-=5.0;
yield;
}
}
}
How to change the button click ins$$anonymous$$d of space
Answer by pagan · Jun 15, 2011 at 01:39 PM
What if I want to modify this script so that the box opens when clickin on it? Thanks